JMESPath · lesson 14 of 15 · Functions

Strings, join & map

join(glue, list) builds a string, and map(&expr, list) applies an expression to every element.

Examples

Example 1Join names into one string

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

join(', ', members[*].name)
  1. join( Glue a list of strings into one string.
  2. ', ' The separator (a raw string).
  3. members[*].name The list to join.

Result

"Ada, Linus, Grace, Alan"

Try it in the playground

Example 2Apply an expression to each element

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

map(&length(skills), members)
  1. map( Run an expression on every element of a list and collect the results.
  2. &length(skills) The expression, as a reference: how many skills each one has.
  3. members The list to map over → [2,2,1,0].

Result

[2,2,1,0]

Try it in the playground

Good to know

  • contains, starts_with, ends_with, length and reverse work on strings too: reverse('abc') is "cba".

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.