JSON functions

Learn about JSON functions in the Digibee Integration Platform and how to use them.

The functions were created to:

  • Speed up the creation of your integrations even more.

  • Decrease the complexity of your pipelines.

  • Simplify data conversions and transformations during the flow of your pipelines.

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.

JSONPATH

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

Syntax

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:

{
  "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:

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

The expected result will be:

{
  "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:

{
  "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:

{
  "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:

{
  "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

TOJSON(value:string)
  • value: string

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

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

The expected result will be:

{
  "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

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:

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

Removing the escape characters:

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

The expected result will be:

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

GETELEMENTAT

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

Syntax

GETELEMENTAT(array, index)

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

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

Capturing the element:

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

The result will be:

{
  "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

LASTELEMENT(array, index)

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

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

Capturing the element:

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

The result will be:

{
  "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

REMOVEAT(array, index)

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

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

Removing the element:

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

The result will be:

{
  "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

PUSH(array, element)

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

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

Inserting a new element:

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

The result will be:

{
  "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

POP(array)

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

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

Removing the last element :

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

The result will be:

{
  "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

NEWEMPTYOBJECT()

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

{
  "data": {
    
  }
}

Creating:

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

The result will be:

{
  "data": {
    
  }
}

NEWEMPTYARRAY

This function allows you to create a new empty array.

Syntax

NEWEMPTYARRAY()

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

{
  "data": [
    
  ]
}

Creating:

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

The result will be:

{
  "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

CARDINALITYONE(data)

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

{
  "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:

{
  "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:

{
  "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

CARDINALITYMANY(data)

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

{
  "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:

{
  "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:

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

Other functions

You can also read about these functions:

Last updated