jq · lesson 7 of 32 · Pipes & building output

Strings & interpolation

"…\(expr)…" inserts the result of any filter into a string. Combine it with join, ascii_downcase, sub and friends for text work.

Examples

Example 1Format a line of text

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) @ \(.office.city), floor \(.office.floor)"
  1. \(.team) Interpolation: anything inside \( ) is a full jq filter whose result is inserted into the string.
  2. \(.office.city) Nested paths work too.
  3. \(.office.floor) Numbers are converted to text automatically.

Result

"Platform @ Berlin, floor 3"

Try it in the playground

Example 2Join and change case

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 | map(.name | ascii_downcase) | join(", ")
  1. map( Apply the filter inside to every member…
  2. ascii_downcase …lower-casing each name…
  3. join(", ") …then glue the array of strings into one string with a separator.

Result

"ada, linus, grace, alan"

Try it in the playground

Good to know

  • More string filters: split(","), ltrimstr, startswith, endswith, test("regex"), length, and sub / gsub for regex replacement.
  • For example .members[0].email | sub("@.*"; "@corp.io") gives "[email protected]".

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.