Find JSON Object

Use this step to locate the first or all objects in a JSON value that meet a specified condition. The search follows a depth-first order. The found objects are stored in a JSON array, which is then returned in a variable. If no matching object is found, an empty array is returned.

Optionally, this step can convert the found objects before storing them in the result array. You can choose to return the JSON paths to the found objects instead of the objects themselves.

Properties

Find In

The input JSON value.

For example, [{"a": 1, "b": 2}, {"a": 3},{"b": 4}]. Note that this value is used in all examples below.

Also, you can configure this property to find in the JSON input parameter or in the variable value.

Search Condition

Specify a search condition in the form of a Boolean expression. The step evaluates this expression for each object in the JSON value to find out if the object should be included in the result.

The expression uses the $object built-in variable to refer to the object.

Consider the following example. Set Find In to [{"a": 1, "b": 2}, {"a": 3},{"b": 4}] and the Search Condition to $object.hasKey("a"). The result will be all objects that have a key called "a": [{"a": 1, "b": 2}, {"a": 3}].

You can narrow down the search result by entering $object.hasKey("a") && $object.a.integer() == 1 to collect all objects that have specified key and value. Thus, the Find JSON Object step collects all objects stored in the JSON value that have a key called "a" with a value "1".

If the Search Condition is set to true, the step collects all the objects in the JSON value.

Find First

Use this option to find and store only the first result.

If selected, the Find JSON Object step stops traversing the JSON tree as soon as it finds an object that fulfills the search condition. The collected object is returned in a JSON array.

If no object is found, the returned array is empty. The search follows a depth-first order.

For example, if the Search Condition is set to true and the Find First option is selected, the Find JSON Object step collects only the first object in the array: [{"a": 1, "b": 2}].

Convert Object

Select this option to convert the found object values into different JSON values before they are stored in the result. For example, you can add extra key with a value to the object, select the value of a given key, remove a key, combine the value with the path where it was found, replace the values, and other. When you select this option, you need to enter an expression. This expression may refer to two built-in variables:

  • $object: Value of the found object.

  • $path: JSON path to the found object. You can later use paths to update the original JSON value.

You can use these variables in combination, such as [$object, $path], and the result will show both the found object and the path to this object.

Use this option to:

  • Convert the values in the found objects immediately by specifying the expression.

    Example:

    With the search condition $object.hasKey("a"), in the Convert Object property specify the expression such as {"a": $object.a.number() + 1}. This adds one to initial values of each key "a": [{"a": 2}, {"a": 4}].

  • Specify the object and/or path to the object that you want to convert and design your robot to convert these values further with the Update JSON step.

    Example:

    With the search condition $object.hasKey("a") and Convert Object configured to:

    • $object.a: The step converts objects that have key "a" to their values: [1, 3].

    • $path: The step converts objects that have key "a" to their paths: ["[0]", "[1]"].

    Later you can add an Update JSON step to replace the values in JSON objects.

Store Result In

Specify a variable of JSON type to store the result of the search.

Find and convert all JSON objects

The example below demonstrates how to use the Find JSON Object step in a scenario where you need to convert object into values in the list, such as names, phone numbers, and other data.

Find JSON Object step

In the "list" input parameter we specify a JSON value in which we want to find all objects and convert all objects with key "n" into their values.

In this example, there are two important conditions:

  • All objects with key "n" are elements of the array.

    This condition ensures that no other object with key "n" gets its value into the list.

  • All objects in the list have a key named "n".

    This condition ensures that the Convert Object expression can be evaluated without errors.

The result is stored in the variable called "result". The new JSON value is displayed as an array in the JSON tab in the Recorder View.