0

I'm trying to process the result of a cURL request, piped into jq, which returns an array of JSON objects like so:

[
  { 
    "first_name" : "John",
    "last_name" : "Doe"

  },
  { 
    "first_name" : "bat",
    "last_name" : "man"

  }
]

Which I then try to format into a JSON array and read through by running

item_list=`curl "https://linkhere.com" | jq`

for item in ${item_list[@]}; do 
    echo "item is $item"
done

However, the items in the array are being read like strings, not JSON objects, outputting:

item is [
item is { 
item is "John":
item is "Doe"
item is }, 
... 

I want to iterate through each object in the JSON array to extract and store the first_name and last_name values. How can I go about properly doing this ?

Barmar
  • 741,623
  • 53
  • 500
  • 612
lem
  • 21
  • 6
  • Show how you're setting `item_list` using `jq`. – Barmar Oct 12 '22 at 21:34
  • JSON is just a string encoding. You have *decode* the string before you actually get a data structure you can traverse, and `bash` doesn't really have that you can map JSON values to. You might use `jq` to parse the JSON and produce something you can process in `bash`, but you probably want to use a different language instead. – chepner Oct 12 '22 at 21:36
  • @chepner I have tried piping the result of the curl req through `jq` by doing `curl ... | jq` in order to create a JSON object, then try to iterate through the array, but the output shown is what happens when I try this approach. – lem Oct 12 '22 at 21:44
  • bash does not know how to work with JSON, only with strings and integers. You'll have to use [tag:jq] to work with the JSON data. What result are you trying to achieve? – glenn jackman Oct 12 '22 at 21:48
  • @Barmar I've edited the question to clarify my approach – lem Oct 12 '22 at 21:50
  • `item_list` isn't an array, it's a string. – Barmar Oct 12 '22 at 21:55
  • 2
    You didn't give any arguments to `jq` that tell it how to extract values from the objects. So the output is the same as the input. What are you expecting that to do? – Barmar Oct 12 '22 at 21:55
  • @Barmar Apologies for the confusion. I was expecting `jq` to format the input into an array of JSON objects that bash can read and iterate through. How can I achieve this ? – lem Oct 12 '22 at 22:03
  • bash can't read JSON objects. That's why you need `jq`. – Barmar Oct 12 '22 at 22:05
  • @lem Try out `readarray -t items < <(curl … | jq -c '.[]')`, and look what happens with `for item in "${items[@]}"; do echo "item is $item"; done` (note the quotes around `${items[@]}`). – pmf Oct 12 '22 at 22:25
  • @pmf thank you! This is exactly what I was looking for! After testing, I believe the `-c` flag allowed jq to properly parse the files in this case. – lem Oct 12 '22 at 23:08
  • @lem jq's `-c` flag puts each result in a separate line (and newline happens to be the default delimiter for `readarray`) – pmf Oct 12 '22 at 23:17

1 Answers1

0

To extract multiple fields, you need to wrap them in quotes within the filter.

.[] | "\(.first_name) \(.last_name)"

Output

John Doe
bat man

https://jqplay.org/s/iH-c-d2sgYm

Otherwise, you can just to .[].first_name to get all first names, for example

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245