# JSON

## 1. JPARSE

Deserializes the specified JSON string into object or array.

{% tabs %}
{% tab title="Usage" %}
`JPARSE(json_string, separator_optional)`&#x20;

Replace:

`json_string` parameter with a JSON format string you need to parse;

`separator_optional` optional parameter to make function convert string to array by splitting it.
{% endtab %}

{% tab title="Example 1" %}
`JPARSE('{"name" : "John Smith", "kids": [{"name": "Jim"}, {"name":"Nicky"}]}')` will return a proper object.
{% endtab %}

{% tab title="Example 2" %}
Let *String\_\_c* = **'\["Feb", "Jan", "Dec"]'**.

`JPARSE({Stub__c.String__c})` returns an array consisting of following elements: **'Feb', 'Jan', 'Dec'**.
{% endtab %}

{% tab title="Example 3" %}
Let *Area\_\_c* = **'Plane; Train; Car'**.&#x20;

`JPARSE({Stub__c.Area__c}, ';')` returns an array consisting of following elements: **Plane, Train, Car.**
{% endtab %}
{% endtabs %}

## 2. JPARSEXML

Deserializes the specified XML string into object.

{% tabs %}
{% tab title="Usage" %}
`JPARSEXML(xml_string)`

Replace `xml_string` parameter with a string (in XML format) you need to parse.
{% endtab %}

{% tab title="Example" %}
`JPARSEXML('<person><address>10 South Riverside Plaza</address><age>9</age><names><first>Jon</first><last>Smith</last></names></person>')` returns an object **person={address=10 South Riverside Plaza, age=9, names={first=Jon, last=Smith}}**.
{% endtab %}
{% endtabs %}

## 3. JOBJECT

Builds an object from provided key names and values.

{% tabs %}
{% tab title="Usage" %}
`JOBJECT(key1, value1, key2, value2, ...)`&#x20;

Replace:

`key1, key2` parameters with strings that are key names

`value1, value2` parameters with strings that are key values

If omit some value, it will be stored as null.
{% endtab %}

{% tab title="Example 1" %}
`JOBJECT()` will return an empty object **{}**.
{% endtab %}

{% tab title="Example 2" %}
`JOBJECT(key1, value1, key2, value2)` will return an object **{“key1“:”value1”, “key2“:”value2”}**
{% endtab %}

{% tab title="Example 3" %}
`JOBJECT(key1, value1, key2)` will return an object **{“key1“:”value1”, “key2“:null}**
{% endtab %}

{% tab title="Example 4" %}

```
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)
)
```

will return an object

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

{% endtab %}
{% endtabs %}

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

* 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.
  {% endhint %}

{% hint style="warning" %}
Empty keys will be ignored
{% endhint %}

## 4. JSTRING

Serializes objects into JSON string.

{% tabs %}
{% tab title="Usage" %}
`JSTRING(json_object)`&#x20;

Replace `json_object` parameter with an object you need to convert into  string.
{% endtab %}

{% tab title="Example" %}
`JSTRING(JEACH(SPLIT('12,14,15', ','), {$JEach}), true))` returns the following string: **'\["12","14","15"]'**.
{% endtab %}
{% endtabs %}

## 5. JGET

Gets a value from JSON content using a path provided.

{% tabs %}
{% tab title="Usage" %}
`JGET(json_object, path)`&#x20;

Replace:

`json_object` parameter with an object you want to use;

`path` parameter with the path to find a field in the object (or pass index for arrays).
{% endtab %}

{% tab title="Example 1" %}
`JGET(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza"}'), 'address')` returns Jon's address value **10 South Riverside Plaza**.
{% endtab %}

{% tab title="Example 2" %}
Let String field value is '**June, July, August**'.

`JGET(JPARSE({Stub__c.String__c}, ','), 2)` returns '**August**'.
{% endtab %}

{% tab title="Example 3" %}
`JGET(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza", "kids": ["Andrew","Jane", "Peter"]}'), JPARSE('["kids", 2]'))` will return '**Peter**' as "*kids*" list has element with index *2*.
{% endtab %}
{% endtabs %}

## 6. JPUT

Puts a value into JSON content using a path provided.

{% tabs %}
{% tab title="Usage" %}
`JPUT(json_object, path, value)`&#x20;

Replace:

`json_object` parameter with an object you want to use;

`path` parameter with the path to find a field in the object to update;

`value` parameter with the value a field will be updated with.&#x20;

{% 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 %}
{% endtab %}

{% tab title="Example 1" %}
`JPUT(JPARSE('{"name" : "Jon Smith"}'), 'address', '10 South Riverside Plaza')` will add Jon's address property and value to JSON object.&#x20;
{% endtab %}

{% tab title="Example 2" %}
`JPUT(JPARSE('["Z", "Y", "X"]'), 0, 'A')` returns an array with following elements: **A, Y, X**.
{% endtab %}
{% endtabs %}

## 7. JREMOVE

Removes a field from JSON content using a path provided.

{% tabs %}
{% tab title="Usage" %}
`JREMOVE(json_object, key_or_jsonobject)`&#x20;

Replace:&#x20;

`json_object` parameter with an object you want to use;

`key_or_jsonobject` parameter with the key to find a field you need to remove or with array element to be removed from JSON array.

{% 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 %}
{% endtab %}

{% tab title="Example 1" %}
`JREMOVE(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza", "age" : 9}'), 'address')` removes an *address* field from the JSON content.
{% endtab %}

{% tab title="Example 2" %}
`JREMOVE(JPARSE('["Z", "Y", "X"]'), 'X')` returns an array with following elements: **Z, Y**.
{% endtab %}

{% tab title="Example 3" %}
`JREMOVE(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza", "age" : 9}'), SPLIT('name,age', ','))` returna a JSON object with address property only.
{% endtab %}

{% tab title="Example 4" %}
`JREMOVE(JPARSE('["1a", "2b", "3c", "4d"]'), SPLIT('1a,2b', ','))` returns an array with following elements: **3c, 4d**.
{% endtab %}
{% endtabs %}

## 8. JCLEAR

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

{% tabs %}
{% tab title="Usage" %}
`JCLEAR(json_object, key_or_jsonobject_optional)`&#x20;

Replace:

`json_object` parameter with an object you want to use;

`key_or_jsonobject_optional` optional parameter with the key to find a field you need to remove or with path to nested object as array of elements.
{% endtab %}

{% tab title="Example 1" %}
Both

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

and

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

will return an empty object **{}**.
{% endtab %}

{% tab title="Example 2" %}
`JCLEAR(JPARSE('{"personnames":{"first":"Jon","last":{"a":"Smith","b":"Ryan"}}, "address": "10 South Riverside Plaza", "age" : 9}'),'personnames')` returns an initial object where nested object '*personnames*' will be empty: **{address=10 South Riverside Plaza, age=9, personnames={}}**
{% endtab %}

{% tab title="Example 3" %}
`JCLEAR(JPARSE('{"personnames":{"first":"Jon","last":{"a":"Smith","b":"Ryan"}}, "address": "10 South Riverside Plaza", "age" : 9}'),JPARSE('["personnames","last"]'))` returns an initial object where 2nd-level nested object '*last*' will be empty: **{address=10 South Riverside Plaza, age=9, personnames={first=Jon, last={}}}**
{% endtab %}
{% endtabs %}

## 9. JSIZE

Returns a number of elements in JSON content.

{% tabs %}
{% tab title="Usage" %}
`JSIZE(json_object)`&#x20;

Replace `json_object` parameter with an object or array you want to get size of.
{% endtab %}

{% tab title="Example 1" %}
`JSIZE(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza"}'))` returns **2** as a number of key-value pairs in JSON content provided.
{% endtab %}

{% tab title="Example 2" %}
`JSIZE(SPLIT('Name, Address, Age, Hair color'), ',')` returns **4**.
{% endtab %}
{% endtabs %}

## 10. JEXIST

Checks if an element exists in JSON.

{% tabs %}
{% tab title="Usage" %}
`JEXIST(json_object, path)`&#x20;

Replace:&#x20;

`json_object` parameter with an object you want to use ;

`path` parameter with the path to find a field you need to check (or index if you work with JSON array).
{% endtab %}

{% tab title="Example 1" %}
`JEXIST(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza"}'), 'Jon Smith')` returns **true** as *Jon Smith* node is present in JSON content.
{% endtab %}

{% tab title="Example 2" %}
`JEXIST(JPARSE('["one" , "two", "three", "four"]'), 2)` returns **true** as element with index *2* is present in list.
{% endtab %}

{% tab title="Example 3" %}
`JEXIST(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza", "appearance": {"eyes":"blue", "hair":"dark"}}'), JPARSE('["appearance", "hair"]'))` returns **true** as "*appearance*" object has key "*hair*".
{% endtab %}

{% tab title="Example 4" %}
`JEXIST(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza", "kids": ["Andrew","Jane", "Peter"]}'), JPARSE('["kids", 2]'))` returns **true** as "*kids*" list has element with index *2*.
{% endtab %}
{% endtabs %}

## 11. JMERGE

Merges 2 JSON objects into one.

{% tabs %}
{% tab title="Usage" %}
`JMERGE(json_object, json_object)`&#x20;

Replace both `json_object` parameters with objects you want to merge.
{% endtab %}

{% tab title="Example" %}
`JMERGE(JPARSE('{"name" : "Jon Smith", "address": "10 South Riverside Plaza"}'), JPARSE('{"appearance": {"eyes":"blue", "hair":"dark"}}'))` will return object **{address=10 South Riverside Plaza, appearance={eyes=blue, hair=dark}, name=Jon Smith}**.
{% endtab %}
{% endtabs %}

## 12. JEACH

Executes specified function for each element of the list.

{% tabs %}
{% tab title="Usage" %}
`JEACH(json_array, function, exclude_nulls_boolean)`&#x20;

Replace:

`json_array` parameter with an array you want to use;

`function` parameter with expression to be executed for each array element;

optional `exclude_nulls_boolean` parameter with **true** to have null values be excluded from the resulting array, or with **false**, if include null values.

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

{% tab title="Example 1" %}
Let *String\_\_c* = **'Andrew,Alex,Helen,Ann,Robert'**.

`JOIN(JEACH(JPARSE({Stub__c.String__c}, ','), IF(STARTS({$JEach}, 'An'), {$JEach}, null), true), ',')` returns the following string '**Andrew, Ann**'.
{% endtab %}

{% tab title="Example 2" %}
Let *String\_\_c* = **'Andrew,Alex,Helen,Ann,Robert'**.

`'[' + JOIN(JEACH(JPARSE({Stub__c.String__c}, ','), {DBL_QUOTE} + TEXT({$JEachIndex}) + : + ' ' + {$JEach} + {DBL_QUOTE}), ',') + ']'` returns the following string '**\["0: 'Andrew","1: Alex","2: Helen","3: Ann","4: Robert"]**'.
{% endtab %}
{% endtabs %}

## 13. JEACHMAP

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

{% tabs %}
{% tab title="Usage" %}
`JEACHMAP(json_object, key_function, value_function)`&#x20;

Replace:

`json_obect` parameter with an object you want to use;

`key_function` parameter with expression to be executed for each key of the object;

`value_function` with expression to be executed for each value of the object.&#x20;

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

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 %}
{% endtab %}

{% tab title="Example 1" %}
`JEACHMAP(JPARSE({$Environment}), {$JEachKey}, IF(INSTANCEOF({$JEach}, Decimal), MULT({$JEach}, 2), {$JEach}))` doubles all numeric values of the *$Environment* context object.
{% endtab %}

{% tab title="Example 2" %}
`JEACHMAP(JPARSE({$Environment}), IF({$JEach} = true, UPPER({$JEachKey}), {$JEachKey}), {$JEach})` converts to uppercase only those object keys which values are equal to **true**.
{% endtab %}
{% endtabs %}

## 14. JVAR

Defines formula local variables.

{% tabs %}
{% tab title="Usage" %}
`JVAR(var1_name, va1_value_function, var2_name, va2_value_function, ..., value_function)`&#x20;

Replace:

`var1_name`, `var2_name` , etc. parameters with variables names;

`va1_value_function`, `va2_value_function`, etc parameters with an expression to be executed as the value for the variables;

`value_function` with an expression to be executed as the result of the function.&#x20;

{% hint style="info" %}
To get access to the variable use the merge field "**{$JVar.var1\_name}**", where "*var1\_name*" is the variable name.
{% endhint %}
{% endtab %}

{% tab title="Example" %}
`JVAR('var1', 1, 'var2', 2, {$JVar.var1} + {$JVar.var2})`- the result of this expression will be "**3**".
{% endtab %}
{% endtabs %}

## 15. JFOR

Executes specified function for each element of the list.

{% tabs %}
{% tab title="Usage" %}
`JFOR(from_decimal, to_decimal, expression, exclude_nulls_boolean_optional)`&#x20;

Replace:

`from_decimal` parameter with a number to start loop from;

`to_decimal` with a number to stop loop at;

`expression` with expression to be executed for each iteration;

optional `exclude_nulls_boolean_optional`  parameter with **true** to have null values be excluded from the result, or with **false**, if include null values.
{% endtab %}

{% tab title="Example" %}
`JFOR(1, 10, TEXT(ADDDAYS({$System.Date}, {$JeachIndex} ,false)), false)` will return list of dates from current date + 1 day to current date + 10 days.
{% endtab %}
{% endtabs %}
