Search code examples
jsonjqscalar

JQ get all simple values in json


I have a json structure which contains nested dictionaries and I want to fetch all the simple values in it (String, Int, bool etc but not dict and lists).

How can I achieve this using JQ.

Example:

{
    "a": 10,
    "b": {
        "d": 20,
        "e": "hello"
    },
    "c": {
        "z": {
            "f": true
        }
    }
}

Expected output not necessarily in this order:

10
20
"hello"
true

Solution

  • Recurse, selecting only the scalar values:

    $ cat tmp.json
    {
      "a": 10,
      "b": {"d": 20, "e": "hello"},
      "c": {"z": {"f": true}}
    }
    $ jq 'recurse | scalars' tmp.json
    10
    20
    "hello"
    true