JSON WalletPrivacy-first JSON & API workspace

Transforming JSON With JSONata

8 min read

← All guides

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)
// 3

Reshaping 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.
  • $sort and 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

Ready to put this into practice? Open the free JSON editor β†’

Press ⌘ K for commands