JMESPath · lesson 11 of 15 · Reshaping

Multiselect hash

{key: expression, …} builds an object, so you can rename, pick and combine values.

Examples

Example 1Rename fields

Document

{
  "team": "Platform",
  "office": { "city": "Berlin", "floor": 3 },
  "members": [
    { "name": "Ada", "age": 36, "role": "lead", "active": true,
      "email": "[email protected]", "skills": ["rust", "sql"] },
    { "name": "Linus", "age": 28, "role": "dev", "active": true,
      "email": null, "skills": ["c", "shell"] },
    { "name": "Grace", "age": 45, "role": "dev", "active": false,
      "skills": ["cobol"] },
    { "name": "Alan", "age": 41, "role": "qa", "active": true,
      "email": "[email protected]", "skills": [] }
  ]
}

Query

members[*].{who: name, years: age}
  1. {who: name, years: age} Build an object for each member: the key who gets the name and years gets the age.

Result

[
  {"who":"Ada","years":36},
  {"who":"Linus","years":28},
  {"who":"Grace","years":45},
  {"who":"Alan","years":41}
]

Try it in the playground

Example 2A summary object

Document

{
  "team": "Platform",
  "office": { "city": "Berlin", "floor": 3 },
  "members": [
    { "name": "Ada", "age": 36, "role": "lead", "active": true,
      "email": "[email protected]", "skills": ["rust", "sql"] },
    { "name": "Linus", "age": 28, "role": "dev", "active": true,
      "email": null, "skills": ["c", "shell"] },
    { "name": "Grace", "age": 45, "role": "dev", "active": false,
      "skills": ["cobol"] },
    { "name": "Alan", "age": 41, "role": "qa", "active": true,
      "email": "[email protected]", "skills": [] }
  ]
}

Query

{team: team, count: length(members), oldest: max_by(members, &age).name}
  1. team: team Copy a value under a key.
  2. count: length(members) A computed value.
  3. oldest: max_by(members, &age).name Functions and paths combine freely.

Result

{"count":4,"oldest":"Grace","team":"Platform"}

Try it in the playground

Good to know

  • Objects come back with their keys in alphabetical order, not the order you wrote them.

About jsonquery

Query, format and learn JSON with jq, JSONPath, JMESPath and JSON Pointer. It is the engine of the desktop app, run in your browser.

Engine
The desktop app’s, compiled to WebAssembly
Licence
MIT (the engine and the app)
Source
GitHub
Contact
The contact form (private)
Public issues
GitHub
Legal
Privacy policy

Known limitations: a file over 10 MB opens read-only and one over 512 MB does not open here; the desktop app has no such limit.