Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON string within JSON and other fields

Tags:

json

bash

jq

I have a file in s3 which when unzipped has something of this format

{"a": "foo",
 "b": "bar",
 "c": : "{\"hello\": \"world\"}"}

Now I know I can parse the value of c by doing jq '.c | fromjson | .hello'

But let's say I want all the values from this json, a, b, and c. This is the code snippet I currently have:

aws s3 cp s3://somebucket/somekey.gz - | gunzip | jq '[.a, .b]'

How do I incorporate grabbing the value from c into this expression?

like image 433
eagle Avatar asked Jan 30 '18 16:01

eagle


1 Answers

I want all the values from this json, a, b, and c

jq solution for valid JSON structure:

... | jq '[.a, .b, (.c | fromjson | .[])]'
  • .[] - returns all the values of the array/object

The output:

[
  "foo",
  "bar",
  "world"
]
like image 104
RomanPerekhrest Avatar answered Sep 28 '22 01:09

RomanPerekhrest