92

I have a JSON data as follows in data.json file

[
  {"original_name":"pdf_convert","changed_name":"pdf_convert_1"},
  {"original_name":"video_encode","changed_name":"video_encode_1"},
  {"original_name":"video_transcode","changed_name":"video_transcode_1"}
]

I want to iterate through the array and extract the value for each element in a loop. I saw jq. I find it difficult to use it to iterate. How can I do that?

Dan Oak
  • 704
  • 1
  • 7
  • 26
kosta
  • 4,302
  • 10
  • 50
  • 104

11 Answers11

163

Just use a filter that would return each item in the array. Then loop over the results, just make sure you use the compact output option (-c) so each result is put on a single line and is treated as one item in the loop.

jq -c '.[]' input.json | while read i; do
    # do stuff with $i
done
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • 8
    A `for` loop iterates over whitespace-separated words, not lines. – chepner Dec 03 '15 at 17:26
  • 1
    Yeah, you're right, though in this specific case, it would have been ok since there were no spaces in any of the objects. But the idea is still the same, the looping mechanism was probably the wrong choice. – Jeff Mercado Dec 03 '15 at 19:22
  • 4
    jq outputs a stream, so you are not going line by line or item by item. – knt5784 Jun 21 '19 at 22:21
  • 4
    If your output contains spaces, you'll need to set your IFS to a newline, e.g with Bash `IFS=$'\n'`. – Andrey Kaipov Aug 24 '20 at 22:33
  • 1
    works for me (Big Sur on Mac). here's what I got so far: `echo "$res" | jq -c -r '.[]' | while read item; do val=$(jq -r '.value' <<< "$item") echo "Value: $val" done` – rv.kvetch Sep 15 '21 at 16:50
  • But, i've found if you're doing stuff like adding to an array within the while loop, you'll need to modify it like `done < <(echo "$res" | jq -c -r '.[]')` instead of piping the output to `while`. See here for more info https://github.com/koalaman/shellcheck/wiki/SC2031 – rv.kvetch Sep 15 '21 at 17:02
  • @JeffMercado I've been using this pretty easily that is until I started calling helper functions in my loop iterations. First one runs fine and then the loop just dies. But echo actually just iterated everything – Malik Brahimi Aug 02 '22 at 03:12
23

jq has a shell formatting option: @sh.

You can use the following to format your json data as shell parameters:

cat data.json | jq '. | map([.original_name, .changed_name])' | jq @sh

The output will look like:

"'pdf_convert' 'pdf_convert_1'"
"'video_encode' 'video_encode_1'",
"'video_transcode' 'video_transcode_1'"

To process each row, we need to do a couple of things:

  • Set the bash for-loop to read the entire row, rather than stopping at the first space (default behavior).
  • Strip the enclosing double-quotes off of each row, so each value can be passed as a parameter to the function which processes each row.

To read the entire row on each iteration of the bash for-loop, set the IFS variable, as described in this answer.

To strip off the double-quotes, we'll run it through the bash shell interpreter using xargs:

stripped=$(echo $original | xargs echo)

Putting it all together, we have:

#!/bin/bash

function processRow() {
  original_name=$1
  changed_name=$2

  # TODO
}

IFS=$'\n' # Each iteration of the for loop should read until we find an end-of-line
for row in $(cat data.json | jq '. | map([.original_name, .changed_name])' | jq @sh)
do
  # Run the row through the shell interpreter to remove enclosing double-quotes
  stripped=$(echo $row | xargs echo)

  # Call our function to process the row
  # eval must be used to interpret the spaces in $stripped as separating arguments
  eval processRow $stripped
done
unset IFS # Return IFS to its original value
Mashmagar
  • 2,556
  • 2
  • 29
  • 38
  • 10
    You can use the `--raw-output` or `-r` flag to exclude the enclosing double quotes, instead of having to 'strip the enclosing double quotes', replacing `jq @sh` with `jq -r @sh` – Cinderhaze Mar 11 '19 at 18:44
  • 2
    You don't (currently) need a shell pipe through a second jq; it works fine to just append `| @sh` in the jq pipeline. As in `jq -r '. | map(blah) | @sh'` – dannysauer Aug 11 '21 at 19:12
23

By leveraging the power of Bash arrays, you can do something like:

# read each item in the JSON array to an item in the Bash array
readarray -t my_array < <(jq --compact-output '.[]' input.json)

# iterate through the Bash array
for item in "${my_array[@]}"; do
  original_name=$(jq --raw-output '.original_name' <<< "$item")
  changed_name=$(jq --raw-output '.changed_name' <<< "$item")
  # do your stuff
done
felipecrs
  • 549
  • 4
  • 16
  • 13
    "Power of Bash Arrays! ⚡️" - It's too much man. – user14492 Jun 29 '21 at 23:45
  • 3
    note to macOS users - this will not work 'out of the box' due to apple sticking with an older version of bash due to licensing (currently v3.2.57). you can use homebrew to obtain the latest version. You will need to set the newer version as your default shell or set your script to use it explicitly with a shebang – Baldy Jul 28 '21 at 15:11
  • Good to know! That must be why macOS switched to ZSH so. – felipecrs Jul 29 '21 at 16:13
  • 3
    And if reading from a variable instead: `readarray -t my_array < <(jq -c '.[]' <<< $input_json)` – mpowrie Sep 07 '21 at 07:56
  • 2
    This is only solution that works out of box. All the others are concepts that need serious correcting to work! – dotokija Dec 09 '21 at 21:58
  • Did not work out-of-the-box for me.. I'm seeing: `syntax error near unexpected token ‘<‘`. Anyone else? So its complaining on the second "<" on the `readarray` command. – Neil P. Oct 19 '22 at 18:13
  • For me it's working just fine, Bash 5.1.16. – felipecrs Oct 19 '22 at 22:44
14

From Iterate over json array of dates in bash (has whitespace)

items=$(echo "$JSON_Content" | jq -c -r '.[]')
for item in ${items[@]}; do
    echo $item
    # whatever you are trying to do ...
done
Tidoni
  • 195
  • 1
  • 4
  • 13
3

Here is a simple example that works in zch shell:

DOMAINS='["google","amazon"]'

arr=$(echo $DOMAINS | jq -c '.[]')
for d in $arr; do
    printf "Here is your domain: ${d}\n"
done
David Dehghan
  • 22,159
  • 10
  • 107
  • 95
  • 1
    This code doesn’t work because you don’t quote `$DOMAINS` in `echo` so Bash interprets `[` as a command and fails. Even if you fixes this, it still breaks if you have a space in one of the domains. – bfontaine Jan 05 '23 at 10:09
  • 1
    sorry. i should have specified that my shell is zch. It works in that shell. – David Dehghan Jan 23 '23 at 05:45
2

Try Build it around this example. (Source: Original Site)

Example:

jq '[foreach .[] as $item ([[],[]]; if $item == null then [[],.[0]]     else [(.[0] + [$item]),[]] end; if $item == null then .[1] else empty end)]'

Input [1,2,3,4,null,"a","b",null]

Output [[1,2,3,4],["a","b"]]

touchStone
  • 317
  • 2
  • 16
  • The original question is vague, but I don't think `foreach` is at all necessary for wha the user wants. – chepner Dec 03 '15 at 17:27
2

None of the answers here worked for me, out-of-the-box.

What did work was a combination of a few:

projectList=$(echo "$projRes" | jq -c '.projects[]')

IFS=$'\n' # Read till newline

for project in ${projectList[@]}; do
  projectId=$(jq '.id' <<< "$project")
  projectName=$(jq -r '.name' <<< "$project")
  ...
done

unset IFS

NOTE: I'm not using the same data as the question does, in this example assume projRes is the output from an API that gives us a JSON list of projects, eg:

{
  "projects": [ 
    {"id":1,"name":"Project"}, 
    ... // array of projects
  ] 
}
Neil P.
  • 1,026
  • 2
  • 15
  • 32
1

An earlier answer in this thread suggested using jq's foreach, but that may be much more complicated than needed, especially given the stated task. Specifically, foreach (and reduce) are intended for certain cases where you need to accumulate results.

In many cases (including some cases where eventually a reduction step is necessary), it's better to use .[] or map(_). The latter is just another way of writing [.[] | _] so if you are going to use jq, it's really useful to understand that .[] simply creates a stream of values. For example, [1,2,3] | .[] produces a stream of the three values.

To take a simple map-reduce example, suppose you want to find the maximum length of an array of strings. One solution would be [ .[] | length] | max.

peak
  • 105,803
  • 17
  • 152
  • 177
0

I stopped using jq and started using jp, since JMESpath is the same language as used by the --query argument of my cloud service and I find it difficult to juggle both languages at once. You can quickly learn the basics of JMESpath expressions here: https://jmespath.org/tutorial.html

Since you didn't specifically ask for a jq answer but instead, an approach to iterating JSON in bash, I think it's an appropriate answer.

Style points:

  1. I use backticks and those have fallen out of fashion. You can substitute with another command substitution operator.
  2. I use cat to pipe the input contents into the command. Yes, you can also specify the filename as a parameter, but I find this distracting because it breaks my left-to-right reading of the sequence of operations. Of course you can update this from my style to yours.
  3. set -u has no function in this solution, but is important if you are fiddling with bash to get something to work. The command forces you to declare variables and therefore doesn't allow you to misspell a variable name.

Here's how I do it:

#!/bin/bash
set -u

# exploit the JMESpath length() function to get a count of list elements to iterate
export COUNT=`cat data.json | jp "length( [*] )"`

# The `seq` command produces the sequence `0 1 2` for our indexes
# The $(( )) operator in bash produces an arithmetic result ($COUNT minus one)
for i in `seq 0 $((COUNT - 1))` ; do

     # The list elements in JMESpath are zero-indexed
     echo "Here is element $i:"
     cat data.json | jp "[$i]"

     # Add or replace whatever operation you like here.

done

Now, it would also be a common use case to pull the original JSON data from an online API and not from a local file. In that case, I use a slightly modified technique of caching the full result in a variable:

#!/bin/bash
set -u

# cache the JSON content in a stack variable, downloading it only once
export DATA=`api --profile foo compute instance list --query "bar"`

export COUNT=`echo "$DATA" | jp "length( [*] )"`
for i in `seq 0 $((COUNT - 1))` ; do
     echo "Here is element $i:"
     echo "$DATA" | jp "[$i]"
done

This second example has the added benefit that if the data is changing rapidly, you are guaranteed to have a consistent count between the elements you are iterating through, and the elements in the iterated data.

Douglas Held
  • 1,452
  • 11
  • 25
0

For the general case, @Jeff's answer is the way to go. It uses jq's --compact-output (or -c) flag to print each iteration result on its own single line, then uses the shell's read function in a while loop to linewise read the results into a shell variable.

jq -c '.[]' input.json | while read i; do
    # do stuff with $i
done

But utilizing that flag comes at the cost of sacrificing the pretty-printing otherwise present in jq's non-compact outputs. If you needed that formatting, the proximate attempt would be to subsequently run other instances of jq on each iteration step to (re-)establish the formatting for each output. However, this can be expensive, especially on large input arrays, and could be generally avoided by retaining the initial formatting while using a delimiter other than the newline character (because the pretty-printed, multi-line output items themselves already do contain newlines characters).

As bash is tagged, one way would be using read's (non-POSIX) -d option to provide a custom delimiter. With an empty string, it defaults to "terminate a line when it reads a NUL character", which can be added to jq's output using "\u0000". As for the jq filter, opening a new context (with |) after the iteration ensures it's printed with every array item. Finally, jq's --join-output (or -j) flag decodes the JSON-encoded NUL character while suppressing the otherwise appended newline characters after each item.

jq -j '.[] | ., "\u0000"' input.json | while read -d '' i; do
    # do stuff with pretty-printed, multi-line "$i"
done
pmf
  • 24,478
  • 2
  • 22
  • 31
-1

This is what I have done so far

 arr=$(echo "$array" | jq -c -r '.[]')
            for item in ${arr[@]}; do
               original_name=$(echo $item | jq -r '.original_name')
               changed_name=$(echo $item | jq -r '.changed_name')
              echo $original_name $changed_name
            done
Rahul
  • 3,293
  • 2
  • 31
  • 43
  • When accessing the value of a key, instead of uisng `. original_name` with no quotes, should it be `original_name =$(echo $item | jq -r '.original_name')`? Also, why is there a space before `=`? – panc Nov 13 '21 at 01:16
  • I don't see how this works at all unless you have `IFS=$'\n'` set before running it, or your JSON objects contain no spaces. And even if you _do_ set IFS, it's still buggy due to the unquoted expansion; if this is run by a shell with `nullglob` or `globfail` flags active you'll get surprises when your JSON contains wildcard characters. – Charles Duffy Feb 02 '22 at 14:33