AgentScript has a built-in json_path() function for extracting data from objects and lists using JSONPath-style expressions.
The basic syntax is:
json_path(variable, path, defaultValue?)
The first argument is the data you want to query. The second is the path that describes what to select. The third is optional and specifies the value to return if no match is found.
A simple example
Let’s say an Agentforce action returns a list of Cases and saves it in a variable:
cases = [
{ "data": {
"CaseNumber": "000123", "Status": "New", "Priority": "High"
} },
{ "data": {
"CaseNumber": "000125", "Status": "Closed", "Priority": "Low"
} },
{ "data":
{ "CaseNumber": "000127", "Status": "New", "Priority": "Medium"
} }
]
You can select a field from the first Case:
json_path(@variables.cases, "$[0].data.CaseNumber")
Result:
["000123"]
$ refers to the object you are querying, it's the root of the path. The return value is always a list.
Working with the whole collection
This is where json_path() becomes more useful. The * wildcard lets you select a field from every item:
json_path(@variables.cases, "$[*].data.CaseNumber")
Result:
["000123", "000125", "000127"]
You can also select a range of items. For example, the first two Cases:
json_path(@variables.cases, "$[0:2]")
Result:
[
{ "data": { "CaseNumber": "000123", "Status": "New", "Priority": "High" } },
{ "data": { "CaseNumber": "000125", "Status": "Closed", "Priority": "Low" } }
]
Filtering the collection
But json_path() can do more than select values — you can also filter a collection based on conditions.
For example, select only Cases with Status = "New":
json_path(@variables.cases, "$[?(@.data.Status == 'New')]")
Result:
[
{ "data": { "CaseNumber": "000123", "Status": "New", "Priority": "High" } },
{ "data": { "CaseNumber": "000127", "Status": "New", "Priority": "Medium" } }
]
You can combine the filter with field selection:
json_path(
@variables.cases,
"$[?(@.data.Status == 'New')].data.CaseNumber"
)
Result:
["000123", "000127"]
And you can use multiple conditions:
json_path(
@variables.cases,
"$[?(@.data.Status == 'New' && @.data.Priority == 'High')].data.CaseNumber"
)
Result:
["000123"]
Here:
?() → filter the collection
@ → the current item
== → compare a value
&& → require both conditions
A note about filtering support:
Salesforce's official documentation currently documents json_path() for accessing list items and using wildcards, but does not document filtering expressions like the examples above.
I tested filtering in a sandbox and it works as expected, but I’d be cautious about relying on it in production - untill Salesforce officially documents it.
AgentScript now has really limited native ways to work directly with collections. The useful part of json_path() is that it gives AgentScript this way to work directly with collections returned by actions — selecting, narrowing, and extracting the data you need without adding Apex or Flow.
Resources:
Reference an Item in a List
Top comments (0)