Skip to content

jq seems to parse string values as json #3067

Closed Answered by itchyny
ephemeris-lappis asked this question in Q&A
Discussion options

You must be logged in to vote

This is not a bug. The for loop splits by white spaces.

json='[ { "Key": "one", "{a=1, b=2}": "ok" }, { "Key": "two", "Text": "ok" } ]'
for x in $(echo "$json" | jq -c '.[]'); do printf "[[[%s]]]\n" "$x"; done
[[[{"Key":"one","{a=1,]]]
[[[b=2}":"ok"}]]]
[[[{"Key":"two","Text":"ok"}]]]

The first input {"Key":"one","{a=1, is not a valid JSON. If you use the compact output option -c, you can read each line, or you can also use --raw-output0 option introduced in 1.7.

while read -r x; do jq . <<< "$x"; done < <(echo "$json" | jq -c '.[]')
while read -r -d '' x; do jq . <<< "$x"; done < <(echo "$json" | jq --raw-output0 '.[]')

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@emanuele6
Comment options

@ephemeris-lappis
Comment options

@wader
Comment options

@ephemeris-lappis
Comment options

@wader
Comment options

Answer selected by emanuele6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants