jQuery有条件地在json数组中添加元素并打印整个修改后的文件

时间:2018-07-20 11:36:59

标签: arrays json add element jq

我有一个具有以下格式的json文件。 如果.children []。type ==“ environmentApprovalTask​​”和.children []。role.name ==“ GCM批准人”并输出,我想在propDefs []中添加元素{“ test”:“ 2”}到新文件。我希望整个文件的内容都已修改。.children[]数组可能并不总是包含3个元素。

{
  "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
      }
    }
  ]
}

我最后一次尝试使用代码

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

这仅产生修改后的元素,没有产生整个文件作为输出。请帮助我如何获得所需的输出。

1 个答案:

答案 0 :(得分:1)

有关jq中的复杂分配的TIL。这实际上有效:

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

与您的版本唯一的区别是作业的左侧带有括号。