Articles in this section

jsonStringify helper

The jsonStringify helper converts a value into JSON text. Use it when a template needs the JSON for an object or array you already have in context, such as a child object taken from a parsed payload.

Note: Always use triple braces ({{{ }}}). Double braces cause Celigo to encode quotes in the JSON, which can make the string invalid for the system that receives it.

Syntax

{{{jsonStringify value}}}

value -- The value to convert. Required. Objects and arrays become compact JSON text, with no extra spaces. A string is returned with its JSON quotes. A number or boolean is returned as its literal JSON text.

Examples

Stringify a child object

{{#with (jsonParse record.payload)}}{{{jsonStringify event}}}{{/with}}

Input:

{
 "record": {
 "payload": "{\"event\":{\"type\":\"create\",\"source\":\"api\"},\"id\":[123,456,789],\"status\":\"success\"}"
 }
}

Output:

{"type":"create","source":"api"}

#with and jsonParse open the stringified payload. jsonStringify writes the event object back out as JSON text.

Stringify an array

{{{jsonStringify record.items}}}

Input:

{
 "record": {
 "items": [
 { "sku": "ITEM001", "quantity": 2 },
 { "sku": "ITEM002", "quantity": 1 }
 ]
 }
}

Output:

[{"sku":"ITEM001","quantity":2},{"sku":"ITEM002","quantity":1}]

Stringify a string

{{{jsonStringify record.email}}}

Input: record.email is "jane.doe@example.com"

Output: "jane.doe@example.com"

The quotes are part of the JSON text. Use the jsonEncode helper when the destination needs the same characters with no surrounding quotes.

Usage notes

  • Always use triple braces ({{{ }}}). Double braces encode the quotes in the result.
  • The JSON is compact. Objects and arrays are not indented, and there is no space after : or ,.
  • A string result includes JSON quotes. A number such as 89.99 returns 89.99, and a boolean returns true or false.
  • jsonParse turns JSON text into an object or array. jsonStringify turns an object or array back into JSON text.
  • jsonSerialize helper is the other helper that writes a JSON string. jsonEncode helper does not serialize objects. An object passed to jsonEncode returns [object Object].