user2882307
user2882307

Reputation:

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

Upvotes: 0

Views: 193

Answers (1)

chepner
chepner

Reputation: 532112

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

Upvotes: 1

Related Questions