jq · lesson 16 of 32 · Transforming

Types & conversion

type names a value's type, and tostring, tonumber, tojson and fromjson convert between types.

Examples

Example 1Inspect and convert

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[0] | (.age | tostring), (.skills | type), ("42" | tonumber)
  1. (.age | tostring) Number → string: "36".
  2. (.skills | type) The type's name: "array".
  3. ("42" | tonumber) String → number: 42. Any literal can be piped into a filter.

Result

"36"
"array"
42

Try it in the playground

Good to know

  • The types are null, boolean, number, string, array and object.
  • Format strings re-encode a value: .name | @base64, @uri, @html, @sh, and @json (the same as tojson).

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.