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 with 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>) -
All attributes from the input FlowFile can be read as an entire object. This is useful, for example, for iterating and filtering over the attributes:
$nfGetAttributes() -
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:
-
JSONatahttps://docs.jsonata.org/overview.html[documentation]
|
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. This makes sense in this case, 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
}
If you want to filter across all attributes to get only some with a prefix, you can do this as described here. The prefix 'http.headers.' used here is used by the HandleHttpRequest processor - so you could only operate on the Http header fields:
$nfGetAttributes().$sift(function($v, $k) { $contains($k, 'http.headers.') }).*
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
}
The function can include any number of name/value pairs in addition to the expression.
These are provided to the Expression Language as temporary attributes for the execution.
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.
Instead of the literal in the last example, the following could also be written:
{
...
"isValidName": $nfEl("${name:matches('^[\\p{L} \\p{Nd}_]+$')}", "name", FirstName & " " & Surname )
...
}