简体   繁体   中英

extract values from JSON using jq

I have a JSON object that looks like this:

{
    "Accounts": [
        {
            "Id": "1",
            "Name": "Joe",
            "Zip": "11111"
        },
        {
            "Id": "2",
            "Name": "Jack",
            "Zip": "22222"
        }
    ]   
}

I am trying to write a jq query that gives me this:

[
    {
        "Id": "1",
        "Name": "Joe"
    },
    {
        "Id": "2",
        "Name": "Jack"
    }   
]

How can I do that? Thanks.

jq '.Accounts | map({ Id, Name })'

Will produce

[
  {
    "Id": "1",
    "Name": "Joe"
  },
  {
    "Id": "2",
    "Name": "Jack"
  }
]

as you can try online using this demo .


  • .Accounts selects the Accounts key
  • map() will apply the following for each object [ docs ]
  • Create object with Id and Name key [docs]

Demo https://jqplay.org/s/v01P2gDVc8

You can do

[.Accounts[] | {Id, Name}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM