JMESPath · lesson 15 of 15 · Functions

Types & defaults

type names a value's type. a || b falls back to b when a is null, missing or empty, and not_null(a, b, …) returns the first argument that isn't null.

Examples

Example 1A default for missing e-mail

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[*].{name: name, email: email || 'n/a'}
  1. email || 'n/a' || returns the right side when the left is null, missing or empty — so Linus and Grace get "n/a".

Result

[
  {"email":"[email protected]","name":"Ada"},
  {"email":"n/a","name":"Linus"},
  {"email":"n/a","name":"Grace"},
  {"email":"[email protected]","name":"Alan"}
]

Try it in the playground

Example 2What type is it?

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

[type(team), type(members), type(members[0].active), type(members[1].email)]
  1. type(team) "string".
  2. type(members) "array".
  3. type(members[0].active) "boolean".
  4. type(members[1].email) "null" — Linus's e-mail is explicitly null.

Result

["string","array","boolean","null"]

Try it in the playground

Good to know

  • Mind the projection: members[*].email || 'n/a' does not default each item — the projection yields a list, and a non-empty list is already truthy. Put the || inside a multiselect hash, as above.
  • to_string(x) and to_number(x) convert: to_number('42') is 42.

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.