0

I have a JSON file like this:

{
 "images1" : 
    {
      "size" : "29x29",
      "idiom" : "iphone",
      "filename" : "[email protected]",
      "scale" : "2x"
    },
 "images2" : 
    {
      "size" : "29x30",
      "idiom" : "iphone2",
      "filename" : "[email protected]",
      "scale" : "22x"
    }
}

I will pass JSON object name as an input. So if I know "images1" is the object, then I need all the keys and values of that object to be stored in two separate arrays, so that I can make use of them in further processing.

Any help is greatly appreciated. Thanks

1 Answer 1

1

You can use the following :

jq ".$1 | { keys: keys_unsorted, values: [.[]] }"

where $1 should provide the name of the item you want to address (note that this assume you're using this in a script. You will probably want to use fedorqui's alternative instead).

It will produce an object whose keys property will be an array of the keys of $1 and values an array of the associated values :

$ echo '{
 "images1" :
    {
      "size" : "29x29",
      "idiom" : "iphone",
      "filename" : "[email protected]",
      "scale" : "2x"
    },
 "images2" :
    {
      "size" : "29x30",
      "idiom" : "iphone2",
      "filename" : "[email protected]",
      "scale" : "22x"
    }
}' | jq ".images1 | { keys: keys_unsorted, values: [.[]] }"
{
  "keys": [
    "size",
    "idiom",
    "filename",
    "scale"
  ],
  "values": [
    "29x29",
    "iphone",
    "[email protected]",
    "2x"
  ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Kudos Aaron..Your solution works for me..Thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.