Output specific key value in object for each element in array with jq for JSON

Solution 1:

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".

Solution 2:

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