JSONataTransformJSON
The JSONataTransformJSON Processor is used to transform JSON using a JSONata script.
Queries or transformations can be executed on incoming content in JSON.
The Processor has the following extra functions in IGUASU:
-
$nfUuid()
generates a UUID of type 4 (pseudo-random number generator). The UUID is generated using a cryptographically strong pseudo-random number generator. -
Attributes from the input FlowFile can be read/processed (in addition to the input content):
$nfGetAttribute(<name>
) -
Results can be written to attributes in addition to or instead of the output content:
$nfSetAttribute(<name>,<value>
-
The NiFi Expression Language can be used:
$nfEl(<expression>)
-
A Lookup Service can be used if this has been defined on the Processor:
$nfLookup(<key>)
The JSONataTransformJSON Processor has a specific editor that allows easy editing of the entire script.
You can quickly try out your transformation by using the "Test/run" button |
More general information about JSONata:
How this Processor implements JSONata transformations is slightly different from try.jsonata.org! |
The advanced functions are now explained in detail.
Simple transformation
Input message (also for the other examples; taken from jsonata.org ):
{
"FirstName": "Fred",
"Surname": "Smith",
"Age": 28,
"Address": {
"Street": "Hursley Park",
"City": "Winchester",
"Postcode": "SO21 2JN"
}
}
Transformation of some of this data into another form of address:
{
"name": FirstName & " " & Surname,
"mobile": Phone[type = "mobile"].number,
"address": Address.City
}
Result:
{
"name": "Fred Smith",
"mobile": "077 7700 1234",
"address": "Winchester"
}
Write result to attributes
If you want to put the same results in attributes instead of in the output content, you can use the following function:
-
nfSetAttribute(<name>,<value>)
In addition, you can deactivate the Processor so that the result of the script is written to the output:
Write Output |
false |
The content is therefore left untouched. In this case, this makes sense, as only the attributes are to be created.
The script now looks like this:
$nfSetAttribute("name", FirstName & " " & Surname) &
$nfSetAttribute("mobile", Phone[type = "mobile"].number) &
$nfSetAttribute("city", Address.City)
The attributes then appear in the result:
name |
Fred Smith |
mobile |
077 7700 1234 |
city |
Winchester |
There is also a property on the Processor to write the entire result of the transformation to an attribute:
Write to Attribute |
<name of attribute> |
Read Attributes
In the first case, for example, if you want to access the attribute filename
to set it as an ID in the result, the script looks like this:
{
"id": $nfGetAttribute("filename"),
"name": FirstName & " " & Surname,
"mobile": Phone[type = "mobile"].number,
"address": Address.City
}
Using the NiFi Expression Language
If you want to use the NiFi Expression Language within a JSONata, this can be done using the corresponding function nfEl(<expression>)
.
In the following example, the NiFi Expression Language is used to check with a regular expression whether the name is correct (i.e. only contains corresponding characters).
{
"name": FirstName & " " & Surname,
"isValidName": $nfEl("${literal('" & FirstName & " " & Surname & "'):matches('^[\\p{L} \\p{Nd}_]+$')}"),
"mobile": Phone[type = "mobile"].number,
"address": Address.City
}
In addition to the expression, the function can include any number of name/value pairs.
These are provided to the Expression Language for execution as temporary attributes.
This means that, unlike $nfSetAttribute(<name>,<value>)
, they are not set beyond the execution.
This can be used, for example, to provide values from the input for the $nfEl()
execution as attributes.
The following could therefore also be written instead of the literal in the last example:
{
...
"isValidName": $nfEl("${name:matches('^[\\p{L} \\p{Nd}_]+$')}", "name", FirstName & " " & Surname )
...
}