The Search/searchNestedJSON extension searches a JSON response for the first object that contains a specified attribute with a specified value.
This is useful when a journey receives a nested JSON response and needs to extract the matching object before using it in later validation or data-handling steps.
Parameters:
-
responserequired, a JSON string representing the response to search through. The script parses this value usingJSON.parse(response); -
searchKeyrequired, a string containing the attribute name to find in the parsed JSON object; -
searchValuerequired, the value that must match the attribute value for the object to be returned;
Note: The response input must be a valid JSON string. If the JSON cannot be parsed, the script cannot continue.
NLP usage
Use the extension in a journey by calling Search/searchNestedJSON with the execute command. Pass each value to the matching extension input using as inputName.
Note: The extension returns the first matching object found during recursive traversal. If no matching object is found, it returns null.
execute "Search/searchNestedJSON" using '{"account":{"code":"LLOYDS","name":"Lloyds Bank"}}' as response using "code" as searchKey using "LLOYDS" as searchValue returning $filteredResponseexecute "Search/searchNestedJSON" using $res as response using "code" as searchKey using "LLOYDS" as searchValue returning $filteredResponseYou can also pass stored variables into the extension inputs.
execute "Search/searchNestedJSON" using "$responseJson" as response using "$attributeName" as searchKey using "$attributeValue" as searchValue returning $filteredResponsestore value '{"customers":[{"name":"Contoso","details":{"code":"LLOYDS","status":"active"}}]}' in $responseJson
store value "code" in $attributeName
store value "LLOYDS" in $attributeValue
execute "Search/searchNestedJSON" using "$responseJson" as response using "$attributeName" as searchKey using "$attributeValue" as searchValue returning $filteredResponseExample output when the nested object contains code with the value LLOYDS:
{
"code": "LLOYDS",
"status": "active"
}This extension does not require any external resource.
The extension should be configured as:
- Run asynchronously: No
- Scope: Global
Limitation: The search uses strict equality with obj[attribute] === value, so the provided searchValue must match the JSON value exactly, including type and casing. The script returns only the first matching object and does not return all matches. It also depends on JSON.parse(response), so invalid JSON input will stop execution.
Add the extension to your Virtuoso instance
Select the domain that matches your Virtuoso account.
View source
Last updated: 25/05/2026
Resources:
This extension does not require any external resource.
// Note this extension is not a product feature of Virtuoso and is not officially supported
// Extensions use javascript which may or may not be compatible with systems under test (SUTs)
// We welcome you to use this extension or adapt it for your needs
/*
The code incorporates recursion to traverse nested objects and arrays, enabling it to search for a specified attribute and value at multiple levels of depth within complex JSON structures. The original code only works for flat JSON structures without nested elements.
*/
const responseJson = JSON.parse(response);
function getObjectWithMatchingAttribute(obj, attribute, value, path = []) {
if (obj && typeof obj === 'object') {
if (obj[attribute] === value) {
return obj;
}
const isPathVisited = path.some((visitedObj) => visitedObj === obj);
if (isPathVisited) {
return null;
}
path.push(obj);
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
const nestedObj = obj[key];
const result = getObjectWithMatchingAttribute(nestedObj, attribute, value, path);
if (result !== null) {
return result;
}
}
}
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
const result = getObjectWithMatchingAttribute(obj[i], attribute, value, path);
if (result !== null) {
return result;
}
}
}
}
return null;
}
/*
let searchKey = 'your_attribute';
let searchValue = 'your_value';
*/
let JsonResponse = getObjectWithMatchingAttribute(responseJson, searchKey, searchValue);
console.log(JsonResponse);
return JsonResponse;
Comments
0 comments
Please sign in to leave a comment.