0

I have following file1 json:

{
  "name": "eye",
  "attributes": [
    {
      "name": "Width",
      "value": "1920"
    },
    {
      "name": "Height",
      "value": "1080"
    },
    {
      "name": "WinKeyMapping",
      "value": "leftwin"
    }
  ],
  "starts": [
    {
      "name": "step1",
      "attributeList": [
        {
          "name": "Command",
          "value": "bash"
        },
        {
          "name": "Test",
          "value": "none"
        }
      ]
    }
  ]
}

and following filter:

$ jq '.starts[].attributeList[]|select(.name=="Command")' file1
{
  "name": "Command",
  "value": "bash"
}
$

how could I get whole structure to this selection?

expected output:

{
  "starts": [
    {
      "attributeList": [
        {
          "name": "Command",
          "value": "bash"
        }
      ]
    }
  ]
}
1

Straightforwardly with jq:

jq '{ starts: [ { attributeList: (.starts[].attributeList 
                                  | map(select(.name == "Command"))) }] }' file.json

The output:

{
  "starts": [
    {
      "attributeList": [
        {
          "name": "Command",
          "value": "bash"
        }
      ]
    }
  ]
}
  • thank you. would it be feasible to "extract" from a huge json all such paths+attributes and save each into separate file named related with the path+attribname - e.g. for example above: starts.attributeList.Command.json ? – DonJ Mar 10 '18 at 13:08
  • @DonJ, welcome, your new description is the case for another question – RomanPerekhrest Mar 10 '18 at 13:30
  • I am trying to collect all attributes which are modified by WebUI (for future usage via REST API). For this case, I GET default json (configuration) and after that did changes via WebUI of the configuration and again GET changed configuration. Thanks to you I have already extracted all attributes from the two big json files to single json files, compared them one by one and now I know which ones were modified (there are tens). do you maybe know how to merge this modified now back to sigle file so I can use it at one REST API PUT command? – DonJ Mar 10 '18 at 15:01
0

Here are two options to obtain your desired output.

You can delete the keys you don't want:

jq 'del(.attributes, .name) | 
  .starts[].attributeList = map(.[].attributeList[] | select(.name == "Command"))'

Or you can select only the key(s) you do want:

jq 'to_entries | map(select(.key == "starts")) | from_entries | 
  .starts[].attributeList = map(.[].attributeList[] | select(.name == "Command"))'

Both give the same result. Also if you wanted starts and ends in the latter case, you could just add additional or statements: map(select((.key == "starts") or (.key == "ends"))).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.