1

I have a json file with the below format. I would like to add the element {"test" : "2"}in propDefs[] if the .children[].type=="environmentApprovalTask" and .children[].role.name== "GCM approver" and output that to the new file. I want the entire file with the modified content.The .children[] array may not have always 3 elements.

{
  "edges": [
    {
      "to": "de32e562319310b7b4fe3736e22009",
      "from": "99d5f0b278f08721adba7741b782d8",
      "type": "SUCCESS",
      "value": ""
    },
    {
      "to": "06916609ad7fd4127815be3f075c81",
      "from": "de32e562319310b7b4fe3736e22009",
      "type": "SUCCESS",
      "value": ""
    },
    {
      "to": "99d5f0b278f08721adba7741b782d8",
      "type": "ALWAYS",
      "value": ""
    }
  ],
  "offsets": [
    {
      "name": "99d5f0b278f08721adba7741b782d8",
      "x": -91,
      "y": 100,
      "h": 70,
      "w": 290
    },
    {
      "name": "06916609ad7fd4127815be3f075c81",
      "x": -5,
      "y": 420,
      "h": 80,
      "w": 120
    },
    {
      "name": "de32e562319310b7b4fe3736e22009",
      "x": 69,
      "y": 240,
      "h": 70,
      "w": 240
    }
  ],
  "layoutMode": "manual",
  "type": "graph",
  "id": "d5d9c4c4-0c5f-4642-872c-ac892039eaa4",
  "name": "79e8b952-dd59-4cfd-9c0c-e6c08a81d4ca",
  "children": [
    {
      "type": "finish",
      "id": "833e959c-6825-413d-afc4-7b74c0a87c3e",
      "name": "06916609ad7fd4127815be3f075c81",
      "children": []
    },
    {
      "id": "e976041d-af9d-48cf-b838-735bb5efd483",
      "type": "envApprovalTask",
      "children": [],
      "name": "de32e562319310b7b4fe3736e22009",
      "roleRestrictionData": {
        "contextType": "ENVIRONMENT",
        "roleRestrictions": [
          {
            "roleId": "087175fb-5d38-42d1-b65a-2b6a6958bc21"
          }
        ]
      },
      "propDefs": [{"test": "1"}],
      "templateName": "ApprovalCreated",
      "commentRequired": false,
      "commentPrompt": "",
      "role": {
        "id": "087175fb-5d38-42d1-b65a-2b6a6958bc21",
        "name": "Approver",
        "isDeletable": true
      }
    },
    {
      "id": "513fbc6a-3c4a-4a10-9eb0-7dc893c69413",
      "type": "environmentApprovalTask",
      "children": [],
      "name": "99d5f0b278f08721adba7741b782d8",
      "roleRestrictionData": {
        "contextType": "ENVIRONMENT",
        "roleRestrictions": [
          {
            "roleId": "116b8cd5-e7e4-403d-9599-35fe25d3cba2"
          }
        ]
      },
      "propDefs": [],
      "templateName": "ApprovalCreated",
      "commentRequired": false,
      "commentPrompt": "",
      "role": {
        "id": "116b8cd5-e7e4-403d-9599-35fe25d3cba2",
        "name": "Manager Approver",
        "isDeletable": true
      }
    }
  ]
}

My final attempt with the code

cat file.json |jq '.|.children[]| select(.type=="envApprovalTask")|select(.role.name=="Approver") |.propDefs[.profDefs|length] |= .+ {"test" : "2"}'

This only produces the modified element, didnt produce the entire file as output. Please help how can i get the desired output.

kanskr
  • 43
  • 1
  • 5

1 Answers1

1

TIL about complex assignments in jq. This actually works:

(.children[] | select(.type=="envApprovalTask" and .role.name=="Approver") | .propDefs) |= .+[{"test":"2"}]

The only significant difference from your version is the parenthesised left side of the assignment.

Michał Politowski
  • 4,288
  • 3
  • 30
  • 41