7

I have a JSON like this

{
  "images" : [
    {
      "size" : "29x29",
      "idiom" : "iphone",
      "filename" : "Icon-Small@2x.png",
      "scale" : "2x"
    }
     ......
     ......
    {
      "size" : "60x60",
      "idiom" : "iphone",
      "filename" : "Icon-60@3x.png",
      "scale" : "3x"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}

I want to iterate through each dictionary in images array. For that I wrote

declare -a images=($(cat Contents.json | jq ".images[]"))
for image in "${images[@]}"
do
    echo "image --$image"
done

I am expecting output that each dictionary is printing in an iteration. That is

image --{
  "size" : "29x29",
  "idiom" : "iphone",
  "filename" : "Icon-Small@2x.png",
  "scale" : "2x"
}
image --{
  "size" : "29x29",
  "idiom" : "iphone",
  "filename" : "Icon-Small@3x.png",
  "scale" : "3x"
}
image --{
  "size" : "40x40",
  "idiom" : "iphone",
  "filename" : "Icon-Spotlight-40@2x.png",
  "scale" : "2x"
}

Etc

But its iterating through each and every single elements in each dictionary like

image --{
image --"size":
image --"29x29",
image --"idiom":
image --"iphone",
image --"filename":
....
....
....

What is wrong with my code

Johnykutty
  • 12,091
  • 13
  • 59
  • 100

1 Answers1

16

The problem with your code is that an array initialization in bash looks like this:

declare -a arr=(item1 item2 item3)

Items are separated by space or newline. You can also use:

declare -a arr(
    item1
    item2
    item3
)

However, the jq output in the example contains both spaces and newlines, that's why the reported behaviour is as expected.


Workaround:

I would get the keys first, pipe them to a read loop and then call jq for each item of the list:

jq -r '.images|keys[]' Contents.json | while read key ; do
    echo "image --$(jq ".images[$key]" Contents.json)"
done

You can also use this jq command if you don't care about pretty printing:

jq -r '.images[]|"image --" + tostring' Contents.json

To access a certain property of the subarray you can use:

jq -r '.images|keys[]' Contents.json | while read key ; do
    echo "image --$(jq ".images[$key].filename" Contents.json)"
done

The above node will print the filename property for each node for example.

However this can be expressed much simpler using jq only:

jq -r '.images[]|"image --" + .filename' Contents.json

Or even simpler:

jq '"image --\(.images[].filename)"' Contents.json
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • The code is working perfectly *image --* is used just for distinguishing new lines wile printing – Johnykutty Dec 30 '15 at 12:26
  • One more doubt, how can I separate each keys from the imageDict – Johnykutty Dec 30 '15 at 12:28
  • I tried jq -r '.images|keys[]' $contentsPath | while read key ; do imageDict=$(jq ".images[$key]" $contentsPath) echo "image --$imageDict" echo "filename --"$("$imageDict" | jq ".filename" ) done but its not working @hek2ml – Johnykutty Dec 30 '15 at 12:29
  • instead of printing whole elements how can I print specific value for key sy filename in the loop – Johnykutty Dec 30 '15 at 12:34
  • +1 Working as expected :) a small side question. if we use like this(the while read method), is it reading from file each time or reading only once? – Johnykutty Dec 30 '15 at 12:44
  • each time in the sense in each iteration – Johnykutty Dec 30 '15 at 12:45
  • Each time. That's the drawback. I would use the `jq`-only solution I've suggested. – hek2mgl Dec 30 '15 at 12:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99307/discussion-between-johnykutty-and-hek2mgl). – Johnykutty Dec 30 '15 at 12:46
  • I also like reading only one, but my need is to read 2-3keys from the dictionary and process them. Anywyay this is working fine – Johnykutty Dec 30 '15 at 12:49