extracting json inside json with jq [duplicate]

The following does the trick:

echo '{"x": "{\"a\": \"b\"}"}' | jq -r .x | jq .a

Here the -r step ensures that the output is raw strings, not JSON texts. Therefore:

"{\"a\": \"b\"}" -> {"a": "b"},

where this “raw string” can subsequently be taken as JSON input for jq .a, returning "b" (or b when used with -r flag).

EDIT: Calling jq twice is not ideal (thanks @peak), the previous command can be combined in this oneliner

| jq -r '.x|fromjson.a'

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top