Transforming JSON With JSONata
8 min read
JSONata is a lightweight query and transformation language designed for JSON. It goes well beyond selection: you can filter, sort, group, run functions and assemble entirely new object and array shapes in a single expression. It powers data mapping in a number of low-code and integration platforms, and you can experiment with it live in the JSONata playground.
The sample data
{
"order": {
"id": "A-100",
"items": [
{ "sku": "pen", "qty": 3, "price": 1.5 },
{ "sku": "note", "qty": 2, "price": 4.0 },
{ "sku": "clip", "qty": 10, "price": 0.2 }
]
}
}Selecting values
order.id
// "A-100"
order.items.sku
// ["pen", "note", "clip"]Notice you do not write array brackets to map over items β JSONata applies the .sku step to every element automatically.
Filtering
order.items[qty > 2]
// the pen and clip line items
order.items[price < 1].sku
// ["clip"]Computing new values
JSONata supports arithmetic across arrays, so you can derive totals inline:
$sum(order.items.(qty * price))
// 17.5 (line totals summed)
$count(order.items)
// 3Reshaping into a new structure
This is where JSONata shines β you can build an object that looks nothing like the input:
{
"orderId": order.id,
"lineCount": $count(order.items),
"total": $sum(order.items.(qty * price)),
"skus": order.items.sku
}produces:
{
"orderId": "A-100",
"lineCount": 3,
"total": 17.5,
"skus": ["pen", "note", "clip"]
}Useful built-in functions
$sum,$average,$max,$min,$countβ aggregation.$uppercase,$lowercase,$trim,$substringβ strings.$map,$filter,$reduceβ higher-order list operations.$sortand the^( )operator β ordering.
JSONata vs JSONPath vs jq
JSONPath only selects values that already exist. jq and JSONata both transform; jq has a Unix-pipeline feel and a command-line heritage, while JSONata reads more like an expression and is popular embedded inside applications and integration tools. For reshaping JSON into a different schema, JSONata is often the most concise of the three.
Frequently asked questions
Why did my expression return nothing?
JSONata returns undefined when a path matches nothing. Matching is case-sensitive β check your field names, and build the expression up one step at a time to see where it stops returning data.
Can JSONata modify the original document?
No. Like most query languages it is read-only: it produces a new result from the input without mutating it. To persist a change, write the result back yourself.
Where can I run JSONata without installing anything?
In the JSONata playground, which evaluates expressions in your browser and updates the result as you type.
Related
Compare approaches in the jq cookbook and querying JSON with JSONPath.
Primary references
This guide is checked against the specifications and runtime documentation below. Examples are also run through JSON Wallet before publication.
Keep reading
What Is JSON? A Beginnerβs Guide
JSON is the most widely used data format on the web. This guide explains what it is, how it is structured, and where you will run into it as a developer.
JSON Syntax Rules Explained (With Examples)
JSON has a small but strict grammar. This reference walks through every rule with valid and invalid examples so your data parses the first time.
Common JSON Errors and How to Fix Them
Every developer hits "Unexpected token in JSON" eventually. Here are the most common JSON errors, what causes them, and exactly how to fix each one.
Ready to put this into practice? Open the free JSON editor β