Docs/Transform and queries
Query and transform JSON
Transform reshapes an array with a query: keep the items you want, sort them and pick the fields to keep. You see the result before it replaces anything.
Run a transform
- Press the Transform button in the panel’s toolbar, or press Ctrl+Alt+Q. It acts on the selected array, or the first array of objects.
- Choose a query language: JSONQuery (the default), JMESPath or JSONPath.
- Use the wizard: tick Filter to keep items that match, tick Sort to order them, and click field chips under Pick to keep only those fields. The query box fills in as you go.
- Or type a query yourself. The preview updates as you type; press Apply transform to replace the part with the preview.
Keys
Query JSON: an example
A JSON query picks out and reshapes part of a document without editing it by hand. Take this array of products: [{"name":"Mug","price":8,"stock":40},{"name":"Lamp","price":35,"stock":0},{"name":"Desk","price":120,"stock":3}].
To keep the products in stock, most expensive first, with only their name and price, the JSONQuery query is filter(.stock > 0) | sort(.price, "desc") | pick(.name, .price). The result is [{"name":"Desk","price":120},{"name":"Mug","price":8}]: the Lamp is gone because its stock is 0.
In JMESPath, reverse(sort_by(@, &price))[*].{name: name, price: price} sorts and picks the same way without the filter, so the Lamp stays in. In JSONPath, $[[email protected] > 0].name selects only the names of the products in stock: ["Mug","Desk"].
JSONPath and JMESPath examples
Each query below runs on the same three products, and each result is what the editor’s Transform preview shows. Paste the array into a panel, press Ctrl+Alt+Q, choose the language and type the query to try it.
- Every name. JSONPath
$[*].name, JMESPath[*].name, JSONQuerymap(.name):["Mug","Lamp","Desk"]. - The first item. JSONPath
$[0]returns a list with one match,[{"name":"Mug","price":8,"stock":40}]; JMESPath[0]returns the object itself. - The last item’s name. JSONPath
$[-1:].namegives["Desk"]; JMESPath[-1].namegives"Desk". - A few items by position. JSONPath
$[0,2].namegives["Mug","Desk"], and the slice$[:2].namegives["Mug","Lamp"]. - Every price at any depth. JSONPath
$..pricegives[8,35,120]. The..searches the whole document, so it also finds prices in nested objects. - Filter by a value. JSONPath
$[[email protected] < 50].nameand JSONQueryfilter(.price < 50) | map(.name)give["Mug","Lamp"]. - Items that are out of stock. JSONPath
$[[email protected] == 0]gives[{"name":"Lamp","price":35,"stock":0}]; JSONQueryfilter(.stock == 0) | map(.name)gives["Lamp"]. - Filter by text in JMESPath.
[?name == 'Desk'].pricegives[120], and[?contains(name, 'a')].namegives["Lamp"]. Text goes between single quotes; a number in a JMESPath filter is written as a JSON literal between backticks. - The most expensive item. JMESPath
max_by(@, &price).namegives"Desk". - A total and a count. JMESPath
sum([*].price)and JSONQuerymap(.price) | sum()give163; JMESPathlength(@)and JSONQuerysize()give3. - Rename fields. JMESPath
[*].{product: name, cost: price}gives[{"product":"Mug","cost":8},{"product":"Lamp","cost":35},{"product":"Desk","cost":120}].
The three languages
- JSONQuery: short pipes, for example
filter(.radiusKm > 3000) | sort(.radiusKm, "desc") | pick(.name, .radiusKm). - JMESPath, as defined at jmespath.org, for example
sort_by([?contains(name, 'ar')], &name)[*].{name: name}. - JSONPath as defined by RFC 9535, for example
$..price. It selects values; sorting and picking are not part of it, so the wizard only fills in the filter.
Queries never run code
All three languages are read by an interpreter that only looks at the data. There is no JavaScript option, and a query can’t reach the page, your storage or the network.
The preview and the applied result come from the same query on the same version of the document, so what you saw is what you get. One Undo reverses it.
Very large numbers
Query languages work with ordinary numbers. Values the query passes through untouched keep their exact text; if a query has to read a number longer than about 16 digits, the dialog says how many were rounded.
When a JSON query helps
- Trim an API response to the fields you need before you share or save it. To turn the result into rows for a spreadsheet, see JSON to CSV: export and import data.
- Reorder a whole array by one field without writing a query: Sort JSON keys and arrays does that in one dialog.
- Keep the original in the other panel and see what the query changed with JSON diff: compare two JSON files.
- A file over 100 MB opens view-only: select the part you need, choose Open selection in the other panel, and query it there; see Open large JSON files online.
- Queries run in your browser, like every tool here, so the JSON you query is not sent to a server.