jq - iterate through dictionaries

Related searches

My json knowledge is shaky, so pardon me if I use the wrong terminology.

I have input.txt which can be simplified down to this:

[
  {
    "foo1": "bar1",
    "baz1": "fizz1"
  },
  {
    "foo2": "bar2",
    "baz2": "fizz2"
  }
]

I want to iterate through each object via a loop, so I'm essentially hoping to tackle just the 1's first, then loop through the 2's, etc.

I thought it was something like:

jq 'keys[]' input.json | while read key ; do
    echo "loop --$(jq "[$key]" input.json)"
done

but that's giving me

loop 0
loop 1

where I would expect to see (spacing here is optional, not sure how jq would parse it):

loop { "foo1": "bar1", "baz1": "fizz1" }
loop { "foo2": "bar2", "baz2": "fizz2" }

What am I missing?

No need to use bash, you can do this in jq itself:

jq -r 'keys[] as $k | "loop: \(.[$k])"' file.json

loop: {"foo1":"bar1","baz1":"fizz1"}
loop: {"foo2":"bar2","baz2":"fizz2"}

Help iterating over keys and values in an object � Issue #73 , ") . jq -n -r '["k3", 4, 5 ,6] | map("\(.) ") | add'� Suppose you want to iterate through a dictionary in Python, but you need to iterate through it repeatedly in a single loop. To get this task done, you can use itertools.cycle(iterable) , which makes an iterator returning elements from iterable and saving a copy of each.

What about using the -c option:

$ jq -c '.[]' file | sed 's/^/loop /'
loop {"foo1":"bar1","baz1":"fizz1"}
loop {"foo2":"bar2","baz2":"fizz2"}

Iterate through dictionaries jq - shell - json - html, "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 � The $.each () function is not the same as $ (selector).each (), which is used to iterate, exclusively, over a jQuery object. The $.each () function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.

Assuming response is a variable containing your data :

echo "$response" | jq --raw-output '.[] | "loop " + tostring'
loop {"foo1":"bar1","baz1":"fizz1"}
loop {"foo2":"bar2","baz2":"fizz2"}

Hope it helps!

Bash for Loop Over JSON Array Using JQ, With JQ and a Bash for loop you can read a JSON config file from Bash and iterate over an array. In this blog post I will explain how this can be� Sometimes you just want to read a JSON config file from Bash and iterate over an array. For example, when seeding some credentials to a credential store . This sometimes can be tricky especially when the JSON contains multi-line strings (for example certificates).

jq print key and value for all in sub-object, Simply pipe to keys function: Sample input.json : { "connections": { "host1": { "ip": " 10.1.2.3" }, "host2": { "ip": "10.1.2.2" }, "host3": { "ip": "10.1.18.1" }� Loop Through a Dictionary. You can loop through a dictionary by using a for loop. When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.

Using jQuery .each() with Arrays. Now, this example is slightly different from the examples above. I previously used a selector with .each() function that would loop through each matching (specified) element on the page. However, now I’ll use $.each() function to iterate through an array and retrieve the values in it.

How to iterate over a C# dictionary? How to access nested Python dictionary items via a list of keys? How to create a directory recursively using Python? How to remove a directory recursively using Python? How to zip a folder recursively using Python? How to scan through a directory recursively in Python? How to define a Python dictionary

Comments
  • Generally, if you want to do something to each object, try to do it in the jq filter. Otherwise, you should probably process the JSON using a language that can decode the JSON into a proper data structure, rather than using the shell.