extracting json inside json with jq

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


from Recent Questions - Stack Overflow https://ift.tt/3kSv8Qk
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Object oriented programming concepts (OOPs)

Spring Boot and Vaadin : Filtering rows in Vaadin Grid