Skip to main content

JSON: advanced features - filtering a list & returning a value

Overview​

You have an item that is returned in a JSON list but rather than being in a specific location in the list i.e. foo.[2].bar - it could be in any location in the list. Using a filter to identify the item, and then extracting that as a variable is how we handle this scenario.

Summary​

The Assertion checks syntax uses a cut-down version of the Jinja templating library to parse the data, which allows for some filters (marked by |) and tests as well as the direct dot-notation syntax.

For example, consider the following JSON:

{
"headers": [
{
"key": "Content-Type",
"value: "application/json"
},
{
"key": "Interaction-Id",
"value: "123456789123456789"
},
{
"key": "X-APImetrics-Id",
"value": "asdfasdfasdfasdf"
}
]
}

In this JSON, the headers are in a list. If that list is always in the same order, you can use the regular notation to find the value associated with the key Interaction-Id:

headers[1].value

However, if this list is not in a guaranteed order, you need to use filters and tests to get the value you require.

In the above example, to get the Interaction-Id, you'd use the following assertion:

headers|selectattr("key", "eq", "Interaction-Id")|map(attribute="value")|first


To explain:

  • the selectattr filters the list of header objects where the object has an attribute key, that is equal to Interaction-Id - this will reduce the headers list to the a list containing the one matching item
  • the map with attribute extracts the value for the filtered list - this will leave a list with one item that is the value we want
  • the first returns the first item in the list

For the full list of filters and tests, see the JSON filters and tests reference.

Use Case​

In this example, we have a sample returned JSON list where we want to find the Interaction ID and extract that as a variable for use in another call.

Sample returned JSON with a headers array containing the Interaction-Id key to be filtered and extracted

The first step is to set up an assertion check to create a filter and check that the item exists.

We do this in the 'assertions' section of the API edit pane.

json.headers|selectattr("key", "eq", "Interaction-Id")|map(attribute="value")|first

We set the ASSERTION to test it exists using the filter string in the test, thus:

Assert row testing that the Interaction-Id selectattr filter expression exists, else set result to Error

We then add the command to EXTRACT the value: First we set the search criteria to find the value Interaction id.

json.headers|selectattr("key", "eq", "Interaction-Id")|map(attribute="value")|first

And then set the EXTRACT conditions to set the variable you use.

Extract row saving the filtered Interaction-Id value as the INTERACTION_ID variable

Your assertions section of the edit pane should look something like this:

Assertions Response Body section showing both the Assert exists check and the Extract to INTERACTION_ID rows

When the monitor is run, the variable INTERACTION_ID is set to the value of the Interaction id in the returned JSON.

This can be used in another call in a workflow using %%INTERACTION_ID%% or INTERATION_ID