I often find myself wanting to parse json with the jq
command, but one of the values inside the json is an escaped string of json.
e.g. if I generate data with python like this:
import json
inner = {'a': 'b'}
outer = {'x': json.dumps(inner)}
json.dumps(outer)
I get:
{"x": "{\"a\": \"b\"}"}
How do I get "b"
using the jq
command?
$ echo '{"x": "{\"a\": \"b\"}"}' | jq .x
"{\"a\": \"b\"}"
Ok, that's the first step. But the result isn't a json dictionary. It's a string that needs to be escaped and parsed into json.
jq .x.a
throws an error: jq: error (at <stdin>:1): Cannot index string with string "a"
Is there an argument I can add to jq
to chain together two jq
steps?
Or is there another command I can use instead?
e.g.
echo '{"x": "{\"a\": \"b\"}"}' | jq .x | jq --some-argument .a
or
echo '{"x": "{\"a\": \"b\"}"}' | jq .x | something-else | jq .a