nora
nora

Reputation: 33

failed to extract data from json with jq command

I've this json. I want to extract test fields which their values equal to true. I tried with jq and got that error, pls any fix ?

$- jq '.[].name | select(.[].test == "true")' ddd                                                                                  

jq: error (at ddd:12): Cannot iterate over string ("AA")

[
{
"name": "AA",
"program_url": "https://www.google.com",
"test": false
},
{
"name": "BB",
"program_url": "https://yahoo.com",
"test": true
}
]

Upvotes: 0

Views: 160

Answers (1)

pmf
pmf

Reputation: 36033

Are you looking for this? It iterates over the array .[], selects those item objects whose .test field evaluates to true (implicit), and traverses further down to the .name field. Using the --raw-output (or -r) option renders the output raw text (instead of JSON string in this case).

jq -r '.[] | select(.test).name' ddd
BB

Demo

Upvotes: 2

Related Questions