Bash脚本-将元素添加到Json数组

06odsfpq  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(201)

我有一个这样的json文件:

{
  "id": 123,
  "guid": "XXXX",
  "isEnabled": true,
  "createdBy": "Admin",
  "updatedBy": "XXXXX",
  "createTime": 987654321,
  "updateTime": 79456123,
  "version": 1,
  "service": "XXXXX",
  "name": "all - consumergroup",
  "policyType": 0,
  "policyPriority": 0,
  "description": "Policy for all - consumergroup",
  "resourceSignature": "XXXXXXXXXX",
  "isAuditEnabled": true,
  "resources": {
    "consumergroup": {
      "values": [
        "*"
      ],
      "isExcludes": false,
      "isRecursive": false
    }
  },
  "policyItems": [
    {
      "accesses": [
        {
          "type": "consume",
          "isAllowed": true
        },
        {
          "type": "describe",
          "isAllowed": true
        }
      ],
      "users": [
        "XXXX",
        "XXXXX",
        "XXXX",
        "XXXXXX",
        "XXXXX",
        "XXXXXX",
        "XXXXXX",
        "usrpfm"
      ],
      "groups": [
        ""
      ],
      "roles": [],
      "conditions": [],
      "delegateAdmin": false
    },
    {
      "accesses": [
        {
          "type": "consume",
          "isAllowed": true
        },
        {
          "type": "describe",
          "isAllowed": true
        },
        {
          "type": "delete",
          "isAllowed": true
        }
      ],
      "users": [
        "rangerlookup"
      ],
      "groups": [
        ""
      ],
      "roles": [],
      "conditions": [],
      "delegateAdmin": false
    }
  ],
  "denyPolicyItems": [],
  "allowExceptions": [],
  "denyExceptions": [],
  "dataMaskPolicyItems": [],
  "rowFilterPolicyItems": [],
  "serviceType": "kafka",
  "options": {},
  "validitySchedules": [],
  "policyLabels": [],
  "zoneName": "",
  "isDenyAllElse": false
}

如何在Bash上添加用户(可能使用jq命令)(例如:BATU)和阵列用户

"users":["XXXX","XXXXX",...,"XXXXXX","usrpfm"]

转向

"users":["XXXX","XXXXX",...,"XXXXXX","usrpfm","BATU"]

这个JSON

jgovgodb

jgovgodb1#

尝试以下操作:

jq  '.policyItems[0].users  |=  . + ["BATUU"]' file.json

使用“|= . +”向policyItems[0].users添加额外的数组索引(BATUU)

相关问题