JSONata Component
Transform data using the JSONata query and transformation language
Component key: jsonata
Description
JSONata is a query and transformation language.
This component takes data and a JSONata expression as input, and returns transformed data based on the expression.
JSONata is helpful when you have a predictable data structure as an input, and you want to output a modified data structure.
JSONata includes common functions you might execute on a data set - things like map
, filter
, sort
, sum
, string.split
(the list goes on).
For example, suppose you had data that read:
{
"example": [{ "value": 4 }, { "value": 7 }, { "value": 13 }]
}
If you applied a JSONata expression that read $sum(example.value)
to this data, 24
would be returned because example.value
generates an array of [4,7,13]
, and $sum
finds the sum of those values.
You can find documentation and examples of JSONata expressions and functions on JSONata's documentation.
The JSONata component is helpful for data transformations, but if you are already comfortable with JavaScript or would like more flexibility in your data transformation, check out the code component instead.
Testing JSONata Expressions
JSONata provides an incredibly useful JSONata test environment where you can try out expressions against data sets. This tool gives you a tight feedback loop, so you can test expressions quickly before pasting them into a JSONata step of an integration.
Some JSONata Examples
Here are a couple examples that show off how to use JSONata expressions to process data:
Concatenating and Filtering Data
In this example, a large block of information about a user is input. The JSONata expression concatenates the user's first and last name, and uses JSONata predicates to filter for the user's mobile phone number:
- Input Data
- JSONata Expression
- Result
{
"FirstName": "Fred",
"Surname": "Smith",
"Age": 28,
"Address": {
"Street": "Hursley Park",
"City": "Winchester",
"Postcode": "SO21 2JN"
},
"Phone": [
{
"type": "home",
"number": "0203 544 1234"
},
{
"type": "office",
"number": "01962 001234"
},
{
"type": "office",
"number": "01962 001235"
},
{
"type": "mobile",
"number": "077 7700 1234"
}
],
"Email": [
{
"type": "office",
"address": ["fred.smith@my-work.com", "fsmith@my-work.com"]
},
{
"type": "home",
"address": ["freddy@my-social.com", "frederic.smith@very-serious.com"]
}
],
"Other": {
"Over 18 ?": true,
"Misc": null,
"Alternative.Address": {
"Street": "Brick Lane",
"City": "London",
"Postcode": "E1 6RF"
}
}
}
{
"name": FirstName & " " & Surname,
"mobile": Phone[type = "mobile"].number
}
{
"name": "Fred Smith",
"mobile": "077 7700 1234"
}
Using Built-in JSONata Functions
In this example, information about a point-of-sale account lists an account name and an array of orders for several products.
The expression uses the built-in $sum
and $map
functions to generate an object containing account name and an array of orders with the total owed for each order.
- Input Data
- JSONata Expression
- Result
{
"Account": {
"Account Name": "Firefly",
"Order": [
{
"OrderID": "order103",
"Product": [
{
"Product Name": "Bowler Hat",
"ProductID": 858383,
"SKU": "0406654608",
"Description": {
"Colour": "Purple",
"Width": 300,
"Height": 200,
"Depth": 210,
"Weight": 0.75
},
"Price": 34.45,
"Quantity": 2
},
{
"Product Name": "Trilby hat",
"ProductID": 858236,
"SKU": "0406634348",
"Description": {
"Colour": "Orange",
"Width": 300,
"Height": 200,
"Depth": 210,
"Weight": 0.6
},
"Price": 21.67,
"Quantity": 1
}
]
},
{
"OrderID": "order104",
"Product": [
{
"Product Name": "Bowler Hat",
"ProductID": 858383,
"SKU": "040657863",
"Description": {
"Colour": "Purple",
"Width": 300,
"Height": 200,
"Depth": 210,
"Weight": 0.75
},
"Price": 34.45,
"Quantity": 4
},
{
"ProductID": 345664,
"SKU": "0406654603",
"Product Name": "Cloak",
"Description": {
"Colour": "Black",
"Width": 30,
"Height": 20,
"Depth": 210,
"Weight": 2
},
"Price": 107.99,
"Quantity": 1
}
]
}
]
}
}
{
"Account": Account.`Account Name`,
"Orders": [
$map(Account.Order, function($v){
{
"id": $v.OrderID,
"total": $sum($map($v.Product, function($p) {
$p.Price * $p.Quantity
}))
}
})
]
}
{
"Account": "Firefly",
"Orders": [
{
"id": "order103",
"total": 90.57
},
{
"id": "order104",
"total": 245.79
}
]
}
Actions
Transform
Transform data using JSONata | key: transform
Input | Notes | Example |
---|---|---|
Data code / Required data | This is an object with properties to feed into the JSONata expression. | |
Expression code / Required expression | A JSONata expression used to generate a string. |
Example Payload for Transform
{
"data": 24
}