The searchValueInJson extension searches a JSON string recursively for objects that contain a matching key-value pair and returns the matching object or objects as an array.
This is useful when a journey receives or stores a larger JSON payload and needs to assert that a specific nested object exists without manually writing multiple path-based assertions.
Parameters:
-
jsonStringrequired, the JSON string to parse and search. The user-facing description may refer to this asjsonData, but the provided source code readsjsonStringdirectly; -
searchKeyrequired, the exact object key to match during the recursive search; -
searchValuerequired, the exact value to match forsearchKey. The source uses strict equality, so type and formatting must match exactly.
Note: Pass the JSON payload as a valid JSON string. If the JSON is malformed, JSON.parse() throws an error and the extension step fails.
How to apply this to your journey
Use the extension in a journey by calling searchValueInJson with the execute command. Pass each value to the matching extension input using as jsonString, as searchKey, and as searchValue.
Note: The search is recursive and returns the full object where the matching key-value pair is found. It does not return the path to the match or perform partial, case-insensitive, or type-coerced matching.
execute "searchValueInJson" using '{ "personalDetails": {"name":"John", "age":30, "city":"New York"}, "payment" : { "frequency": "monthly", "amount": "8000", "currency": "USD"} }' as jsonString and "name" as searchKey and "John" as searchValue returning $result
assert $result[0].name equals "John"execute "searchValueInJson" using '{ "personalDetails": {"name":"John", "age":30, "city":"New York"}, "payment" : { "frequency": "monthly", "amount": "8000", "currency": "USD"} }' as jsonString and "amount" as searchKey and "8000" as searchValue returning $result
assert $result[0].currency equals "USD"You can also pass values stored in variables. This is useful when the JSON payload is produced by an earlier API, extension, or stored-value step.
store value '{ "personalDetails": {"name":"John", "age":30, "city":"New York"}, "payment" : { "frequency": "monthly", "amount": "8000", "currency": "USD"} }' in $jsonString
store value "name" in $searchKey
store value "John" in $searchValue
execute "searchValueInJson" using $jsonString as jsonString and $searchKey as searchKey and $searchValue as searchValue returning $result
assert $result[0].name equals "John"The setup below stores a nested JSON payload and searches for the payment object by matching frequency with monthly.
store value '{ "personalDetails": {"name":"John", "age":30, "city":"New York"}, "payment" : { "frequency": "monthly", "amount": "8000", "currency": "USD"} }' in $jsonString
store value "frequency" in $searchKey
store value "monthly" in $searchValueExample output when the matching object is found:
[
{
"frequency": "monthly",
"amount": "8000",
"currency": "USD"
}
]This extension does not require any external resource.
The extension should be configured as:
- Run asynchronously: No
- Scope: Global
Limitation: This extension parses the supplied input with JSON.parse(jsonString), so the input must be a valid JSON string rather than a pre-parsed object unless Virtuoso has converted it to a string before execution. The search walks nested objects and arrays using for...in, returns the containing object for each exact key/value match, and uses strict equality (===), so 30 does not match "30", matching is case-sensitive, and partial text matching is not supported. If a match is inside an array item, the returned result is that item object, not the parent array or a JSON path. Large or deeply nested JSON strings can take longer to parse and traverse, and circular structures are not supported because the input must be JSON text. The source catches parsing/search errors and rethrows them, so malformed JSON or unexpected data shapes fail the step. Cross-browser note: This extension uses standard JavaScript parsing and recursion rather than page DOM APIs, so browser differences are limited in normal use. Still, validate it in the browser/device configurations used by your plans when the JSON comes from page state, API responses, environment variables, or other journey steps that may format values differently across executions.
Add the extension to your Virtuoso instance
Select the domain that matches your Virtuoso account.
View source
Last updated: 30/04/2025
Resources:
This extension does not require any external resource.
// Last updated: 30/04/2025, 06:26:31 UTC
function searchNestedJSON(jsonString, searchKey, searchValue) {
const jsonData = JSON.parse(jsonString);
const results = [];
function search(obj) {
for (const key in obj) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
search(obj[key]);
} else if (key === searchKey && obj[key] ===
searchValue) {
results.push(obj);
}
}
}
search(jsonData);
return results;
}
try{
return searchNestedJSON(jsonString, searchKey, searchValue);
} catch(error) {
throw new Error(error)
}
Comments
0 comments
Please sign in to leave a comment.