82

I have an array:

[
    {
        "AssetId": 14462955,
        "Name": "Cultural Item"
    },
    {
        "AssetId": 114385498,
        "Name": "Redspybot"
    },
    {
        "AssetId": 29715011,
        "Name": "American Cowboy"
    },
    {
        "AssetId": 98253651,
        "Name": "Mahem"
    }
]

I would like to loop through each object in this array, and pick out the value of each key called AssetId and output it. How would I do this using jq for the command line?

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
Zimbabwe Elephant
  • 965
  • 1
  • 8
  • 12
  • Please make it more clear whether it is a [jq] question or [jquery] question. They are _not_ the same and should not be used together... Your question is completely ambiguous in how you tagged it and asked it. – Jeff Mercado Feb 28 '16 at 02:45

5 Answers5

98

The command-line tool jq writes to STDOUT and/or STDERR. If you want to write the .AssetId information to STDOUT, then one possibility would be as follows:

jq -r ".[] | .AssetId" input.json

Output:

14462955
114385498
29715011
98253651

A more robust incantation would be: .[] | .AssetId? but your choice will depend on what you want if there is no key named "AssetId".

peak
  • 105,803
  • 17
  • 152
  • 177
  • Note: How would I send each one into a function? – Zimbabwe Elephant Feb 28 '16 at 11:48
  • After trying this, I found an error. It says unexpected string. – Zimbabwe Elephant Feb 28 '16 at 11:53
  • @ZimbabweElephant - please see update. If you are still having problems, then please be more specific and/or create a new SO Q as appropriate. – peak Feb 28 '16 at 17:44
  • @ZimbabweElephant - not sure what "function" you are referring to. One that you create yourself in jq? – peak Feb 28 '16 at 18:03
  • This just gives a string output. You can't put this in a bash for loop. – Craig Jun 21 '17 at 15:48
  • @Craig - There was nothing in the question about a bash for loop, but if someone wants to feed the selected values to a bash for loop, the usual way would be to use the idiom: .... | while read -r line ; do ... ; done – peak Jun 21 '17 at 18:16
  • Shoot @peak, you're right. I was pulling my hair out today trying to figure out how to loop through a json array, and when I solved it, I decided to go back and post my answer in the places that didn't quite answer my question. This thread was not the right place to post. I'll remove my answer from this thread. – Craig Jun 22 '17 at 00:27
26

You can also do it via this command.

jq ".[].AssetId" input.json

if array like be that which is in my case

{  
   "resultCode":0,
   "resultMsg":"SUCCESS",
   "uniqueRefNo":"111222333",
   "list":[  
      {  
         "cardType":"CREDIT CARD",
         "isBusinessCard":"N",
         "memberName":"Bank A",
         "memberNo":10,
         "prefixNo":404591
      },
      {  
         "cardType":"DEBIT CARD",
         "isBusinessCard":"N",
         "memberName":"Bank A",
         "memberNo":10,
         "prefixNo":407814
      },
      {  
         "cardType":"CREDIT CARD",
         "isBusinessCard":"N",
         "memberName":"Bank A",
         "memberNo":10,
         "prefixNo":413226
      }
   ]
}

you can get the prefixNo with below jq command.

jq ".list[].prefixNo" input.json

For more specific case on array iterating on jq you can check this blogpost

user-id-14900042
  • 686
  • 4
  • 17
erhun
  • 3,549
  • 2
  • 35
  • 44
7

you have a couple of choices to do the loop itself. you can apply peak's awesome answer and wrap a shell loop around it. replace echo with the script you want to run.

via xargs

$ jq -r ".[] | .AssetId" input.json | xargs -n1 echo  # this would print
14462955
114385498
29715011
98253651

via raw loop

$ for i in $(jq -r ".[] | .AssetId" input.json)
  do
    echo $i
  done
14462955
114385498
29715011
98253651
Alexander Oh
  • 24,223
  • 14
  • 73
  • 76
4

An alternative using map:

jq "map ( .AssetId ) | .[]"
Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
3

For your case jq -r '.[].AssetId' should work

You can also use online JQ Parser : https://jqplay.org/

enter image description here

If you want to loop through the each value then can use below :

for i in $(echo $api_response | jq -r ".[].AssetId")
  do
    echo echo $i 
  done
Afsar Ali
  • 555
  • 6
  • 17