Closed nabadeep closed 10 years ago
Looks like you have a JSON string inside of your JSON. Checkout fromjson
:
jq '.result.data[0].notes | fromjson | .recordCountValidation ' < input.txt
input.txt looks like this:
{
"result": {
"data": [
{
"notes": "{\"recordCountValidation\":\"failure\",\"Metafile Md5\":\"d41d8cd98f00b204e9800998ecf8427e\",\"schemaValidation\":\"failure\"}"
}
]
}
}
It could be a feature of jq to decode
@pasamio What could be a feature? fromjson
is there now, but jq can't be expected to detect that a string's content is a JSON text and automatically decode it...
@nicowilliams sorry, just saw this. I wasn't suggesting it should be expected to detect it, I was suggesting that the OP could use fromjson
to retrieve the content they're after.
@pasamio Ah, sry.
jq
is amazing! :)
I have an array:
[
{
"foo": "bar"
"bat": "bin"
"json": "{\"key\": \"value\" …}"
},
…
]
And I would like to transform it to:
{
foo: "bar"
bar: bat
json: {
key: "value",
…
}
}
…
fromjson
doesn't appear to work like '.[] | {foo, bar, json: fromjson(json)}'
. What should I be reaching for here?
ah. apparently .[] | {foo, bar, json: (.json | fromjson)}
is the magic trick.
No magic, just pipes and closures. The magic would have been the transmogrification of "bat":"bin"
to bar:bat
, except it was evidently just a typo :-)
How is this for magic? (I use this to make Terraform output, AWS cli output, and AWS logs readable. One command to beautify them all!
From @pasamio's example:
## Input:
jq 'walk(if type == "string" and .[0:2] == "{\"" then .=(.|fromjson) else . end)' <<EOF
{
"result": {
"data": [
{
"notes": "{\"recordCountValidation\":\"failure\",\"Metafile Md5\":\"d41d8cd98f00b204e9800998ecf8427e\",\"schemaValidation\":\"failure\"}"
}
]
}
}
EOF
## Output:
{
"result": {
"data": [
{
"notes": {
"recordCountValidation": "failure",
"Metafile Md5": "d41d8cd98f00b204e9800998ecf8427e",
"schemaValidation": "failure"
}
}
]
}
}
From @timvisher's example:
## Input:
jq 'walk(if type == "string" and .[0:2] == "{\"" then .=(.|fromjson) else . end)' <<EOF
[
{
"foo": "bar",
"bat": "bin",
"json": "{\"key\": \"value\"}"
}
]
EOF
## Output:
[
{
"foo": "bar",
"bat": "bin",
"json": {
"key": "value"
}
}
]
@RichardBronosky - Note that .=(.|fromjson)
can be simplified to fromjson
. (How's that for magic?)
jq '.result.data[0].notes' fetches
"{\"recordCountValidation\":\"failure\",\"Metafile Md5\":\"d41d8cd98f00b204e9800998ecf8427e\",\"schemaValidation\":\"failure\"}"
Can somebody fetch me the things further --- the recordCountValidation's value as failure or, the shemaValidation's value as failure.