> For the complete documentation index, see [llms.txt](https://docs.work-relay.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.work-relay.com/automations/functions-and-formulas/functions-library/json-functions.md).

# JSON functions

<details>

<summary>JPARSE</summary>

Deserializes the specified JSON string into **object** or **array**.

**Usage:**

{% code overflow="wrap" %}

```apex
JPARSE(json_string, separator_optional) 
```

{% endcode %}

*<mark style="color:cyan;">Replace</mark>* *<mark style="color:cyan;">`json_string`</mark>* *<mark style="color:cyan;">parameter with a JSON format string you need to parse.</mark>*\
*<mark style="color:cyan;">Pass</mark>* *<mark style="color:cyan;">`separator_optional`</mark>* *<mark style="color:cyan;">string parameter to make JPARSE convert string to array by splitting it with this separator.</mark>*

**Example 1:**

{% code overflow="wrap" %}

```apex
JPARSE('{"name" : "John Smith", "kids": [{"name": "Jim"}, {"name":"Nicky"}]}') 
```

{% endcode %}

Returns an object.

**Example 2:**

<mark style="color:orange;">Let String value is following: '\["Feb", "Jan", "Dec"]'.</mark>

{% code overflow="wrap" %}

```apex
JPARSE({Stub__c.String__c}) 
```

{% endcode %}

Returns an array consisting of following elements: 'Feb', 'Jan', 'Dec'.

**Example 3:**

<mark style="color:orange;">Let Area\_\_c = 'Plane; Train; Car'.</mark>

{% code overflow="wrap" %}

```apex
JPARSE({Stub__c.Area__c}, ';') 
```

{% endcode %}

Returns an array consisting of following elements: Plane, Train, Car.

</details>

<details>

<summary>JOBJECT</summary>

Builds an **object** from provided key names and values.

**Usage:**

{% code overflow="wrap" %}

```apex
JOBJECT(key1, value1, key2, value2, ...) 
```

{% endcode %}

*<mark style="color:cyan;">Replace:</mark>*

* *<mark style="color:cyan;">`key1`</mark><mark style="color:cyan;">,</mark>* *<mark style="color:cyan;">`key2`</mark><mark style="color:cyan;">, etc. parameters with strings that are key names</mark>*
* *<mark style="color:cyan;">`value1`</mark><mark style="color:cyan;">,</mark>* *<mark style="color:cyan;">`value2`</mark><mark style="color:cyan;">, etc. parameters with strings that are key values. If omit some value, it will be stored as</mark>* *<mark style="color:cyan;">**null**</mark>*

**Example 1:**

{% code overflow="wrap" %}

```apex
JOBJECT() 
```

{% endcode %}

Returns an empty object `{}`.

**Example 2:**

{% code overflow="wrap" %}

```apex
JOBJECT(key1, value1, key2, value2) 
```

{% endcode %}

Returns an object `{"key1":"value1", "key2":"value2"}`.

**Example 3:**

{% code overflow="wrap" %}

```apex
JOBJECT(key1, value1, key2) 
```

{% endcode %}

Returns an object `{"key1":"value1", "key2":null}`.

**Example 4:**

```apex
JOBJECT(
  truevalue, true,
  falsevalue, false,
  evaluatedtruevalue, 1 = 1,
  evaluatedfalsevalue, 1 = 0,
  emptyvalue, ,
  emptystringvalue,'',
  nullvalue2, null,
  numbervalue, 17,
  negativevalue, -456.45,
  jsonvalue, JOBJECT(my2key1, my2value1, my2key2, my2value2)
)
```

Returns the following object:

```json
{
  "jsonvalue":{
    "my2key2":"my2value2",
    "my2key1":"my2value1"
  },
  "negativevalue":-456.45,
  "numbervalue":17,
  "nullvalue2":null,
  "emptystringvalue":"",
  "emptyvalue":null,
  "evaluatedfalsevalue":false,
  "evaluatedtruevalue":true,
  "falsevalue":false,
  "truevalue":true
}
```

{% hint style="info" %}
Object values depend on data passed to the function:

* blank space and `null` will give **null** value
* logical expression and `true` will give **true** value. **False** is got in the same way.
* if value is numeric (or has numeric result of calculation), it will be given as a **number**, without quotes.
* to make empty string value, pass `''`.
* to make a nested object, call **JOBJECT** function in the proper place.

Empty keys will be ignored.
{% endhint %}

</details>

<details>

<summary>JSTRING</summary>

Serializes objects into JSON string.

**Usage:**

{% code overflow="wrap" %}

```apex
JSTRING(json_object) 
```

{% endcode %}

*<mark style="color:cyan;">Replace</mark>* *<mark style="color:cyan;">`json_object`</mark>* *<mark style="color:cyan;">parameter with an object (e.g., converted from some other values) you need to convert into string.</mark>*

**Example:**

{% code overflow="wrap" %}

```apex
JSTRING(JEACH(SPLIT('12,14,15', ','), {$JEach}), true)) 
```

{% endcode %}

Returns the string '**\["12","14","15"]**'.

</details>

<details>

<summary>JGET</summary>

Gets a value from JSON content using a path provided.

**Usage:**

{% code overflow="wrap" %}

```apex
JGET(json_object, path) 
```

{% endcode %}

*<mark style="color:cyan;">Replace</mark>* *<mark style="color:cyan;">`json_object`</mark>* *<mark style="color:cyan;">parameter with an object you want to use and path parameter with the path to find a field in the object or pass index for arrays.</mark>*

**Example 1:**

{% code overflow="wrap" %}

```apex
JGET(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza"}'), 'address') 
```

{% endcode %}

Returns Jon's address value "**10 South Riverside Plaza**".

**Example 2:**

<mark style="color:orange;">Let String field value is 'June, July, August'.</mark>

{% code overflow="wrap" %}

```apex
JGET(JPARSE({Stub__c.String__c}, ','), 2) 
```

{% endcode %}

Returns '**August**'.

**Example 3:**

{% code overflow="wrap" %}

```apex
JEXIST(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza", "kids": ["Andrew","Jane", "Peter"]}'), JPARSE('["kids", 2]')) 
```

{% endcode %}

Returns **true** as "kids" list has element with index 2.

</details>

<details>

<summary>JPUT</summary>

Puts a value into JSON content using a path provided.

**Usage:**

{% code overflow="wrap" %}

```apex
JPUT(json_object, path, value) 
```

{% endcode %}

*<mark style="color:cyan;">Replace:</mark>*

* *<mark style="color:cyan;">`json_object`</mark>* *<mark style="color:cyan;">parameter with an object you want to use</mark>*
* *<mark style="color:cyan;">`path`</mark>* *<mark style="color:cyan;">parameter with the path to find a field in the object to update it</mark>*
* *<mark style="color:cyan;">`value`</mark>* *<mark style="color:cyan;">parameter which field in the object should be updated with</mark>*

{% hint style="info" %}
You can also pass **index** as a path if you operate with an array.

If value needs to be added as a last element of an array pass `-1` as a path.
{% endhint %}

**Example 1:**

{% code overflow="wrap" %}

```apex
JPUT(JPARSE('{"name" : "Jon Smith"}'), 'address', '10 South Riverside Plaza') 
```

{% endcode %}

Adds Jon's address property and value to JSON object.

**Example 2:**

{% code overflow="wrap" %}

```apex
JPUT(JPARSE('["Z", "Y", "X"]'), 0, 'A') 
```

{% endcode %}

Returns an array with following elements: **A, Y, X**.

</details>

<details>

<summary>JREMOVE</summary>

Removes a field from JSON content using a path provided.

**Usage:**

{% code overflow="wrap" %}

```apex
JREMOVE(json_object, key_or_jsonobject) 
```

{% endcode %}

*<mark style="color:cyan;">Replace:</mark>*

* *<mark style="color:cyan;">`json_object`</mark>* *<mark style="color:cyan;">parameter with an object you want to use</mark>*
* *<mark style="color:cyan;">`key_or_jsonobject`</mark>* *<mark style="color:cyan;">parameter with the key to find a field you need to remove or with array element to be removed from JSON array</mark>*

**Example 1:**

{% code overflow="wrap" %}

```apex
JREMOVE(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza", "age" : 9}'), 'address') 
```

{% endcode %}

Removes an address field from the JSON content.

**Example 2:**

{% code overflow="wrap" %}

```apex
JREMOVE(JPARSE('["Z", "Y", "X"]'), 'X') 
```

{% endcode %}

Returns an array with following elements: **Z, Y**.

{% hint style="info" %}
If you need to remove multiple values from object you can pass array of object keys as a `key_or_jsonobject`.

Same is true for JSON arrays: if you need to remove several elements from JSON array pass an array of elements to be removed.
{% endhint %}

**Example 3:**

{% code overflow="wrap" %}

```apex
JREMOVE(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza", "age" : 9}'), SPLIT('name,age', ',')) 
```

{% endcode %}

Returns a JSON object with address property only.

**Example 4:**

{% code overflow="wrap" %}

```apex
JREMOVE(JPARSE('["1a", "2b", "3c", "4d"]'), SPLIT('1a,2b', ',')) 
```

{% endcode %}

Returns an array with following elements: **3c, 4d**.

</details>

<details>

<summary>JCLEAR</summary>

Clears a whole JSON object or nested object using a path provided.

**Usage:**

{% code overflow="wrap" %}

```apex
JCLEAR(json_object, key_or_jsonobject_optional) 
```

{% endcode %}

*<mark style="color:cyan;">Replace:</mark>*

* *<mark style="color:cyan;">`json_object`</mark>* *<mark style="color:cyan;">parameter with an object you want to use</mark>*
* *<mark style="color:cyan;">`key_or_jsonobject_optional`</mark>* *<mark style="color:cyan;">parameter with the key to find a field you need to remove or with path to nested object as array of elements</mark>*

**Example 1:**

{% code overflow="wrap" %}

```apex
JCLEAR(JPARSE('{"personnames":{"first":"Jon","last":{"a":"Smith","b":"Ryan"}}, "address": "10 South Riverside Plaza", "age" : 9}')) 
```

{% endcode %}

and

{% code overflow="wrap" %}

```apex
JCLEAR(JPARSE('{"personnames":{"first":"Jon","last":{"a":"Smith","b":"eee"}}, "address": "10 South Riverside Plaza", "age" : 9}'),null) 
```

{% endcode %}

both return an empty object **{}**.

**Example 2:**

{% code overflow="wrap" %}

```apex
JCLEAR(JPARSE('{"personnames":{"first":"Jon","last":{"a":"Smith","b":"Ryan"}}, "address": "10 South Riverside Plaza", "age" : 9}'),'personnames') 
```

{% endcode %}

Returns an initial object where nested object `personnames` will be empty:&#x20;

{% code overflow="wrap" %}

```json
{"address":"10 South Riverside Plaza", "age":9, "personnames":{}}
```

{% endcode %}

**Example 3:**

{% code overflow="wrap" %}

```apex
JCLEAR(JPARSE('{"personnames":{"first":"Jon","last":{"a":"Smith","b":"Ryan"}}, "address": "10 South Riverside Plaza", "age" : 9}'),JPARSE('["personnames","last"]')) 
```

{% endcode %}

Returns an initial object where 2nd-level nested object `last` will be empty:&#x20;

{% code overflow="wrap" %}

```json
{"address":"10 South Riverside Plaza", "age":9, "personnames":{"first":"Jon", "last":{}}}
```

{% endcode %}

</details>

<details>

<summary>JSIZE</summary>

Provides a number of elements in JSON content.

**Usage:**

{% code overflow="wrap" %}

```apex
JSIZE(json_object) 
```

{% endcode %}

*<mark style="color:cyan;">Replace</mark>* *<mark style="color:cyan;">`json_object`</mark>* *<mark style="color:cyan;">parameter with an object or array you want to get size of.</mark>*

**Example 1:**

{% code overflow="wrap" %}

```apex
JSIZE(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza"}')) 
```

{% endcode %}

Returns **2** as a number of key-value pairs in JSON content provided.

**Example 2:**

{% code overflow="wrap" %}

```apex
JSIZE(SPLIT('Name, Address, Age, Hair color', ',')) 
```

{% endcode %}

Returns **4**.

</details>

<details>

<summary>JEXIST</summary>

Checks if an element exists in JSON.

**Usage:**

{% code overflow="wrap" %}

```apex
JEXIST(json_object, path) 
```

{% endcode %}

*<mark style="color:cyan;">Replace:</mark>*

* *<mark style="color:cyan;">`json_object`</mark>* *<mark style="color:cyan;">parameter with an object you want to use</mark>*
* *<mark style="color:cyan;">`path`</mark>* *<mark style="color:cyan;">parameter with the path to find a field you need to check, and index if you work with JSON array</mark>*

**Example 1:**

{% code overflow="wrap" %}

```apex
JEXIST(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza"}'), 'Jon Smith') 
```

{% endcode %}

Returns **true** as Jon Smith node is present in JSON content.

**Example 2:**

<pre class="language-apex" data-overflow="wrap"><code class="lang-apex"><strong>JEXIST(JPARSE('["one" , "two", "three", "four"]'), 2) 
</strong></code></pre>

Returns **true** as element with index 2 is present in list.

**Example 3:**

{% code overflow="wrap" %}

```apex
JEXIST(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza", "appearance": {"eyes":"blue", "hair":"dark"}}'), JPARSE('["appearance", "hair"]')) 
```

{% endcode %}

Returns **true** as "appearance" object has key "hair".

**Example 4:**

{% code overflow="wrap" %}

```apex
JEXIST(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza", "kids": ["Andrew","Jane", "Peter"]}'), JPARSE('["kids", 2]')) 
```

{% endcode %}

Returns **true** as "kids" list has element with index 2.

</details>

<details>

<summary>JMERGE</summary>

Merges 2 JSON objects into one.

**Usage:**

{% code overflow="wrap" %}

```apex
JMERGE(json_object, json_object) 
```

{% endcode %}

*<mark style="color:cyan;">Replace both</mark>* *<mark style="color:cyan;">`json_object`</mark>* *<mark style="color:cyan;">parameters with objects you want to merge.</mark>*

**Example:**

{% code overflow="wrap" %}

```apex
JMERGE(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza"}'), JPARSE('{"appearance": {"eyes":"blue", "hair":"dark"}}')) 
```

{% endcode %}

Returns an object `{"address":"10 South Riverside Plaza","appearance":{"eyes":"blue", "hair":"dark"}, "name":"Jon Smith"}`.

</details>

<details>

<summary>JEACH</summary>

Executes specified function for each element of the list.

**Usage:**

{% code overflow="wrap" %}

```apex
JEACH(json_array, function, exclude_nulls_boolean)
```

{% endcode %}

*<mark style="color:cyan;">Replace:</mark>*

* *<mark style="color:cyan;">`json_array`</mark>* *<mark style="color:cyan;">parameter with an array you want to use</mark>*
* *<mark style="color:cyan;">`function`</mark>* *<mark style="color:cyan;">parameter with expression to be executed for each array element</mark>*
* *<mark style="color:cyan;">`exclude_nulls_boolean`</mark>* *<mark style="color:cyan;">with boolean (this is an optional parameter). If set it to</mark>* *<mark style="color:cyan;">**true**</mark>* *<mark style="color:cyan;">nulls will be excluded from the resulting array</mark>*

{% hint style="info" %}
You can get current element by using the merge field "**{$JEach}**" and you can get current element index by using the merge field "**{$JEachIndex}**".
{% endhint %}

**Example 1:**

<mark style="color:orange;">Let String = 'Andrew,Alex,Helen,Ann,Robert'.</mark>

{% code overflow="wrap" %}

```apex
JOIN(JEACH(JPARSE({Stub__c.String__c}, ','), IF(STARTS({$JEach}, 'An'), {$JEach}, null), true), ',') 
```

{% endcode %}

Returns a string '**Andrew, Ann**'.

**Example 2:**

{% code overflow="wrap" %}

```apex
'[' + JOIN(JEACH(JPARSE({Stub__c.String__c}, ','), {DBL_QUOTE} + TEXT({$JEachIndex}) + : + ' ' + {$JEach} + {DBL_QUOTE}), ',') + ']' 
```

{% endcode %}

Returns a string '**\["0: 'Andrew","1: Alex","2: Helen","3: Ann","4: Robert"]**'.

</details>

<details>

<summary>JEACHMAP</summary>

Executes specified functions for each key and/or value of the json object.

**Usage:**

{% code overflow="wrap" %}

```apex
JEACHMAP(json_object, key_function, value_function)
```

{% endcode %}

*<mark style="color:cyan;">Replace:</mark>*

* *<mark style="color:cyan;">`json_obect`</mark>* *<mark style="color:cyan;">parameter with an object you want to use</mark>*
* *<mark style="color:cyan;">`key_function`</mark>* *<mark style="color:cyan;">parameter with expression to be executed for each key of the object</mark>*
* *<mark style="color:cyan;">`value_function`</mark>* *<mark style="color:cyan;">with expression to be executed for each value of the object</mark>*

{% hint style="info" %}
If you don't need to trigger function execution for object keys pass `{$JEachKey}` as a `key_function` parameter, or if no function needs to be run for object values - pass `{$JEach}` as `value_function` parameter.

You can get current element by using the merge field `{$JEach}` and you can get current element index by using the merge field `{$JEachIndex}`.
{% endhint %}

**Example 1:**

{% code overflow="wrap" %}

```apex
JEACHMAP(JPARSE({$Environment}), {$JEachKey}, IF(INSTANCEOF({$JEach}, Decimal), MULT({$JEach}, 2), {$JEach})) 
```

{% endcode %}

Doubles all numeric values of the $Environment context object.

**Example 2:**

{% code overflow="wrap" %}

```apex
JEACHMAP(JPARSE({$Environment}), IF({$JEach} = true, UPPER({$JEachKey}), {$JEachKey}), {$JEach}) 
```

{% endcode %}

Converts to uppercase only those object keys which values are **true**.

</details>

<details>

<summary>JVAR</summary>

Defines formula local variables.

**Usage:**

{% code overflow="wrap" %}

```apex
JVAR(var1_name, va1_value_function, var2_name, va2_value_function, ...,value_function)
```

{% endcode %}

*<mark style="color:cyan;">Replace:</mark>*

* *<mark style="color:cyan;">`var1_name`</mark><mark style="color:cyan;">,</mark>* *<mark style="color:cyan;">`var2_name`</mark><mark style="color:cyan;">, etc. parameters with an variables names</mark>*
* *<mark style="color:cyan;">`va1_value_function`</mark><mark style="color:cyan;">,</mark>* *<mark style="color:cyan;">`va2_value_function`</mark><mark style="color:cyan;">, etc. parameters with an expression to be executed as the value for the variables</mark>*
* *<mark style="color:cyan;">`value_function`</mark>* *<mark style="color:cyan;">with an expression to be executed as the result of the function</mark>*

{% hint style="info" %}
To get access to the variable you should use the merge field `{$JVar.var1_name}`, where `var1_name` is the variable name.
{% endhint %}

**Example:**

{% code overflow="wrap" %}

```apex
JVAR('var1', 1, 'var2', 2, {$JVar.var1} + {$JVar.var2}) 
```

{% endcode %}

Returns **3**.

</details>

<details>

<summary>JFOR</summary>

Iterates through list/object using specified index range and executes specified function for each element of the list.

**Usage:**

{% code overflow="wrap" %}

```apex
JFOR(from_decimal, to_decimal, expression, exclude_nulls_boolean_optional)
```

{% endcode %}

*<mark style="color:cyan;">Replace:</mark>*

* *<mark style="color:cyan;">`from_decimal`</mark>* *<mark style="color:cyan;">parameter with a number to start loop from</mark>*
* *<mark style="color:cyan;">`to_decimal`</mark>* *<mark style="color:cyan;">with a number to stop loop at</mark>*
* *<mark style="color:cyan;">`expression`</mark>* *<mark style="color:cyan;">parameter with expression to be executed for each iteration</mark>*
* *<mark style="color:cyan;">`exclude_nulls_boolean_optional`</mark>* *<mark style="color:cyan;">with boolean value (this is an optional parameter). If set it to</mark>* *<mark style="color:cyan;">**true**</mark>* *<mark style="color:cyan;">nulls will be excluded from the result map</mark>*

**Example:**

{% code overflow="wrap" %}

```apex
JFOR(1, 10, TEXT(ADDDAYS({$System.Date}, {$JeachIndex} ,false)), false) 
```

{% endcode %}

Returns list of dates from current date + 1 day to current date + 10 days.

</details>
