Examples
These examples show the functionality of JSON Transform node.
Example 1: If the template is set
as
{
"literal": "a literal",
"field": "@@$.id",
"var": "%my_var%",
"array_of_objects": [ "@@$.phones" ],
"array_of_primitives": [ "@@$.numbers" ],
"object": { "*":"@@$.address" }
}
and the given JSON expression passed to the input of the
node:
{
"id": 123,
"name": "fred",
"address": {
"street": "101 Main St",
"city": "London"
},
"numbers": [ 1, 2, 3 ],
"phones": [
{
"type": "mobile",
"number": "111-222-3333"
},
{
"type": "home",
"number": "444-444-5555"
}
]
}
the node will generate this as the
output:
"literal": "a literal",
"field": 123,
"var": "Value of my_var",
"array_of_objects": [
{
"type": "mobile",
"number": "111-222-3333"
},
{
"type": "home",
"number": "444-444-5555"
}
],
"array_of_primitives": [
1,
2,
3
],
"object": {
"street": "101 Main St",
"city": "London"
}
}
To create this output, the node traversed the template to build the output. It produced these fields:
- literal – The value was set to a literal value.
- field – The value of the field is obtained from the path expression
$.id
, which has the value 123 in the input document. - var – The value is specified as the name of a flow variable named city, the value of which is set in the output.
- array_of_objects – The value of the field is obtained from the path
expression
$.phones
. Because the template specified this path within an array, the path expression is expected to return an array. - array_of_primitives – An array of primitive values is returned by
specifying the path expression
$.numbers
within array syntax. - object – An object is replaced by the fetched value by specifying the
special key value "*" in the object. If this is found, the object in the template is replaced by the
object returned by the
$.address
path.
Example 2: This example demonstrates the query syntax.
To determine the ID of the service named “Upload”.
The JSON sent to the node
is:
{
"services": [
{
"_id": "5f9ae9765bffe304360d10d6",
"name": "Upload"
},
{
"_id": "5f9ae9765bffe304565656b3",
"name": "Download"
}
]
}
The template contains a query expression (as indicated by the
'?')
{
"id": "@@$.services[?(@.name == \"Upload\")]._id"
}
This query finds the object in the "services" array which has its "name" field set to "Upload".
It then returns the "_id" field from that object to produce this
output:
{"id":"5f9ae9765bffe304360d10d6"}