jqlang / jq

Command-line JSON processor
https://jqlang.github.io/jq/
Other
30.44k stars 1.58k forks source link

Unable to extract strings from a json object using jq #282

Closed nabadeep closed 10 years ago

nabadeep commented 10 years ago

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.

pasamio commented 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

nicowilliams commented 10 years ago

@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...

pasamio commented 10 years ago

@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.

nicowilliams commented 10 years ago

@pasamio Ah, sry.

timvisher commented 8 years ago

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?

timvisher commented 8 years ago

ah. apparently .[] | {foo, bar, json: (.json | fromjson)} is the magic trick.

pkoppstein commented 8 years ago

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 :-)

RichardBronosky commented 5 years ago

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"
    }
  }
]
pkoppstein commented 5 years ago

@RichardBronosky - Note that .=(.|fromjson) can be simplified to fromjson. (How's that for magic?)