# JSON functions

The JSON functions make operations in JSON objects and are available for components that support Double Braces expressions. To know how to provide information to the components using this resource, refer to [Double Braces Functions](https://docs.digibee.com/documentation/connectors-and-triggers/double-braces/double-braces-functions).

## JSONPATH

This function in Double Braces returns parts of a determined document according to the sent expression.

### Syntax

```json
JSONPATH(value:string, expression:string)
```

* **value:** string
* **expression:** string containing the expression to be recovered

Let's say you need to obtain part of a document using expressions. The document contains the following information, which can be passed through the output of the last component or the payload:

```json
{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ]
  }
}
```

Getting the information:

```json
{
  "test": {{ JSONPATH(message.$, "$.store.book[?(@.price > 10)]") }},
  "test2": {{ JSONPATH(message.store.book,"$.[0]" ) }}
}
```

The expected result will be:

```json
{
  "test": [
    {
      "category": "fiction",
      "author": "Evelyn Waugh",
      "title": "Sword of Honour",
      "price": 12.99
    },
    {
      "category": "fiction",
      "author": "J. R. R. Tolkien",
      "title": "The Lord of the Rings",
      "isbn": "0-395-19395-8",
      "price": 22.99
    }
  ],
  "test2": {
    "category": "reference",
    "author": "Nigel Rees",
    "title": "Sayings of the Century",
    "price": 8.95
  }
}
```

It's possible to use the functions that JSONPATH internally provides, such as:

* `min()`
* `max()`
* `avg()`
* `stddev()`
* `length`

#### Example usage

Let's say that an input array is received:

```json
{
  "numbers": [
    1,
    33,
    56,
    77.8,
    66,
    10475,
    665464,
    1,
    0.01
  ]
}
```

And that you want to use the functions described above in the “expression” field:

```json
{
  "min": {{ JSONPATH(message.numbers, "$.min()") }},
  "max": {{ JSONPATH(message.numbers, "$.max()") }},
  "avg": {{ JSONPATH(message.numbers, "$.avg()") }},
  "stddev": {{ JSONPATH(message.numbers, "$.stddev()") }},
  "length": {{ JSONPATH(message.numbers, "$.length()") }}
}
```

The result will be:

```json
{
  "min": 0.01,
  "max": 665464.0,
  "avg": 75130.42333333334,
  "stddev": 208739.83034832493,
  "length": 9
}
```

## TOJSON

This function in Double Braces returns the JSON value from the received object.

#### Syntax

```json
TOJSON(value:string)
```

* **value:** string

Let's say you need to obtain the values of a JSON object. You can do the following:

```json
{ 
  "test1": {{ TOJSON(111) }},
  "test2": {{ TOJSON("false") }},
  "test3": {{ TOJSON("{\"name\":\"John\",\"age\":31,\"city\":\"New York\"}") }},
  "test4": {{ TOJSON(null) }}
}
```

The expected result will be:

```json
{
  "test1": 111,
  "test2": false,
  "test3": {
    "name": "John",
    "age": 31,
    "city": "New York"
  },
  "test4": null
}
```

## UNESCAPEJSON

This function in Double Braces returns a JSON by removing the found escapes in the provided value.

### Syntax

```json
UNESCAPE(value:string, type:optional)
```

* **value:** string
* **type (optional):** CSV, HTML, XML, and JSON. JSON is the default value.

Let's say you need to remove escape characters of the following payload:

{% code overflow="wrap" %}

```json
{
  "jsonEscaped": "{\\\"name\\\":\\\"John\\\",\\\"age\\\":31,\\\"city\\\":\\\"New York\\\"}"
}
```

{% endcode %}

Removing the escape characters:

```json
{
  "unescaped": {{ UNESCAPEJSON(message.jsonEscaped) }}
} 
```

The expected result will be:

```json
{
  "unescaped": "{\"name\":\"John\",\"age\":31,\"city\":\"New York\"}"
}
```

## GETELEMENTAT

This function allows you to capture a specific element from an array.

### Syntax

```json
GETELEMENTAT(array, index)
```

Let's say you need to capture the first element of the array below:

```json
{
  "data": [
    {
      "field": "value-1"
    },
    {
      "field": "value-2"
    },
    {
      "field": "value-3"
    }
  ]
}
```

Capturing the element:

```json
{
  "element": {{ GETELEMENTAT(message.data, 0) }}
}
```

The result will be:

```json
{
  "element": {
    "field": "value-1"
  }
}
```

Se a função receber um índice inexistente, o valor `null` será retornado.

## LASTELEMENT

This function allows you to capture the last element from an array.

### Syntax

```json
LASTELEMENT(array, index)
```

Let's say you need to capture the last element of the array below:

```json
{
  "data": [
    {
      "field": "value-1"
    },
    {
      "field": "value-2"
    },
    {
      "field": "value-3"
    }
  ]
}
```

Capturing the element:

```json
{
  "element": {{ LASTELEMENT(message.data) }}
}
```

The result will be:

```json
{
  "element": {
    "field": "value-3"
  }
}
```

If the function receives an empty array, a `null` value will be returned.

## REMOVEAT

This function allows you to remove a specific element from an array.

### Syntax

```json
REMOVEAT(array, index)
```

Let's say you need to remove the second element of the array below:

```json
{
  "data": [
    {
      "field": "value-1"
    },
    {
      "field": "value-2"
    },
    {
      "field": "value-3"
    }
  ]
}
```

Removing the element:

```json
{
  "data": {{ REMOVEAT(message.data, 1) }}
}
```

The result will be:

```json
{
  "data": [
    {
      "field": "value-1"
    },
    {
      "field": "value-3"
    }
  ]
}
```

If the function receives an inexistent index, the original array will be returned with no changes.

## PUSH

This function allows you to add an element at the end of an array.

### Syntax

```json
PUSH(array, element)
```

Let's say you need to insert a new element in the following array :

```json
{
  "element": {
    "example": "123"
  },
  "array": [
    {
      "example": "ABC"
    },
    {
      "example": "FFF"
    }
  ]
}
```

Inserting a new element:

```json
{
  "array": {{ PUSH(message.array, message.element) }}
}
```

The result will be:

```json
{
  "array": [
    {
      "example": "ABC"
    },
    {
      "example": "FFF"
    },
    {
      "example": "123"
    }
  ]
}
```

If a nonexistent or null element is added, the value null will be inserted at the end of the array.

## POP

This function allows you to remove the last element from an array. The result will be the element removed and the remaining array.

### Syntax

```json
POP(array)
```

Let's say you need to remove the last element from the following array:

```json
{
  "array": [
    {
      "id": "ABC"
    },
    {
      "id": "FFF"
    },
    {
      "id": "123"
    }
  ]
}
```

Removing the last element :

```json
{
  "data": {{ POP(message.array) }}
}
```

The result will be:

```json
{
  "data": {
    "array": [
      {
        "id": "ABC"
      },
      {
        "id": "FFF"
      }
    ],
    "element": {
      "id": "123"
    }
  }
}
```

If the array is empty, the result will be the same empty array.

## NEWEMPTYOBJECT

This function allows you to create a new empty object.

### Syntax

```json
NEWEMPTYOBJECT()
```

Let's say you need to create a new empty object:

```json
{
  "data": {
    
  }
}
```

Creating:

```json
{
  "data": {{ NEWEMPTYOBJECT() }}
}
```

The result will be:

```json
{
  "data": {
    
  }
}
```

## NEWEMPTYARRAY

This function allows you to create a new empty array.

### Syntax

```json
NEWEMPTYARRAY()
```

Let's say you need to create a new empty array:

```json
{
  "data": [
    
  ]
}
```

Creating:

```json
{
  "data": {{ NEWEMPTYARRAY() }}
}
```

The result will be:

```json
{
  "data": [
    
  ]
}
```

## CARDINALITYONE

The function allows a cardinality of n:1 to be applied to any informed structure, where regardless of the number of elements in the input, the output will always be 1 element.

### Syntax

```json
CARDINALITYONE(data)
```

Let's say you need to apply a n:1 cardinality on each element from the JSON below:

```json
{
  "arrayObject": [
    {
      "id": "PROD-123",
      "type": "Product"
    },
    {
      "id": "CUST-456",
      "type": "Customer"
    }
  ],
  "arrayNumber": [
    23,
    790
  ],
  "emptyArray": [
    
  ],
  "object": {
    "test": "value"
  },
  "singleNumber": 500,
  "singleString": "CARDINALITY",
  "nullValue": null,
  "emptyObject": {
    
  }
}
```

Applying the function:

```json
{
  "arrayObject": {{ CARDINALITYONE(message.arrayObject) }},
  "arrayNumber": {{ CARDINALITYONE(message.arrayNumber) }},
  "emptyArray": {{ CARDINALITYONE(message.emptyArray) }},
  "object": {{ CARDINALITYONE(message.object) }},
  "singleNumber": {{ CARDINALITYONE(message.singleNumber) }},
  "singleNumber": {{ CARDINALITYONE(message.singleNumber) }},
  "singleString": {{ CARDINALITYONE(message.singleString) }},
  "emptyObject": {{ CARDINALITYONE(message.emptyObject) }}
}
```

The result will be:

```json
{
  "arrayObject": {
    "id": "PROD-123",
    "type": "Product"
  },
  "arrayNumber": 23,
  "emptyArray": null,
  "object": {
    "test": "value"
  },
  "singleNumber": 500,
  "singleString": "CARDINALITY",
  "emptyObject": {
    
  }
}
```

## CARDINALITYMANY

This function allows an output to be standardized by a multiple cardinality. That means, when an input of an array has n elements, its output will be an array of n elements, and when an input has a sole object, its output will be an array of this same object.

### Syntax

```json
CARDINALITYMANY(data)
```

Let's say you need to apply a m:n cardinality to each element from the JSON below:

```json
{
  "arrayObject": [
    {
      "id": "PROD-123",
      "type": "Product"
    },
    {
      "id": "CUST-456",
      "type": "Customer"
    }
  ],
  "arrayNumber": [
    23,
    790
  ],
  "emptyArray": [
    
  ],
  "object": {
    "test": "value"
  },
  "singleNumber": 500,
  "singleString": "CARDINALITY",
  "nullValue": null,
  "emptyObject": {
    
  }
}
```

Applying the function:

```json
{
  "arrayObject": {{ CARDINALITYMANY(message.arrayObject) }},
  "arrayNumber": {{ CARDINALITYMANY(message.arrayNumber) }},
  "emptyArray": {{ CARDINALITYMANY(message.emptyArray) }},
  "object": {{ CARDINALITYMANY(message.object) }},
  "singleNumber": {{ CARDINALITYMANY(message.singleNumber) }},
  "singleNumber": {{ CARDINALITYMANY(message.singleNumber) }},
  "singleString": {{ CARDINALITYMANY(message.singleString) }},
  "emptyObject": {{ CARDINALITYMANY(message.emptyObject) }}
}
```

The result will be:

```json
{
  "arrayObject": [
    {
      "id": "PROD-123",
      "type": "Product"
    },
    {
      "id": "CUST-456",
      "type": "Customer"
    }
  ],
  "arrayNumber": [
    23,
    790
  ],
  "emptyArray": [
    
  ],
  "object": [
    {
      "test": "value"
    }
  ],
  "singleNumber": [
    500
  ],
  "singleString": [
    "CARDINALITY"
  ],
  "emptyObject": [
    {
      
    }
  ]
}
```
