【问题标题】:Add and modify JSON object using jq使用 jq 添加和修改 JSON 对象
【发布时间】:2018-04-03 12:12:08
【问题描述】:

示例 JSON 输入:

 {  
"Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowFullAccess",
      "Effect": "Allow",
      "Principal": {
        "AWS": [
          "arn:aws:iam::XXXX:user/test",
          "arn:aws:iam::XXXX:root"
        ]
      },
      "Action": "s3:*",
      "Resource": [
        "arn:aws:s3:::test-dev-cognito-settings-us-west-2/*"
      ],
      "Condition": {
        "StringNotLike": {
          "aws:userId": [
            "AZASDASDSADA"
          ]
        }
      }
    }
  ]
}

预期的 JSON 输出:

  {  
"Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowFullAccess",
      "Effect": "Allow",
      "Principal": {
        "AWS": [
          "arn:aws:iam::XXXX:user/test",
          "arn:aws:iam::XXXX:root"
        ]
      },
      "Action": "s3:*",
      "Resource": [
        "arn:aws:s3:::test-dev-cognito-settings-us-west-2/*"
      ],
      "Condition": {
        "StringNotLike": {
          "aws:userId": [
            "AZALEA",
            "Hello"
          ]
        }
      }
},
{
  "Sid": "AllowForSpecificLambda_jdtest",
  "Effect": "Allow",
  "Principal": {
    "AWS": "AROAIBA5TVJCIN3OCE2YI"
  },
  "Action": "s3:Get*",
  "Resource": [
    "arn:aws:s3:::oppscience-dev-cognito-settings-us-west-2",
    "arn:aws:s3:::oppscience-dev-cognito-settings-us-west-2/*"
  ],
  "Condition": {
    "StringNotLike": {
      "aws:userId": [
        "AZA"
      ]
    }
  }
 ]
}

对不起,我在 json 标签中犯了一些语法错误。 我想要的只是在我想要添加新对象+修改现有对象的语句数组对象中。 我正在使用jq 添加新的 JSON 对象。下面是我的代码 sn-p 工作正常。

jq '.Statement[.Statement| length] |= . + {
 "Sid": "AllowForSpecificLambda",
 "Effect": "Allow",
 "Principal": {
    "AWS": [
        "arn:aws:iam::XXXXXXXXXX:role/lambda_allow_pretoken_generation"
    ]
   },
 "Action": "s3:Get*","Resource": [
        "arn:aws:s3:::test-XXXX-cognito-settings-'$region'"

        ]}' test.json > test-1.json

我正在使用下面的代码 sn-p 在我的 JSON 数组中添加新值。

jq '.Statement[] 

| select(.Sid == "Test") 
.Condition.StringNotLike."aws:userId"[.Condition.StringNotLike."aws:userId"| length] 
|= . + "Hello"' test.json

如何在单个命令中完成这两件事?

谢谢

【问题讨论】:

  • 最好把初始json数据和最终预期结果贴出来
  • 当你想要do this two things in single command 并且你使用的命令是jq 时,为什么要用sed 标记它?
  • 给定的期望输出中没有任何带有“Test”或“Hello”的内容,因此不清楚“新值”应该去哪里。
  • @peak 我已经添加了应附加在输出中的所需“Hello”。谢谢!

标签: shell sed jq


【解决方案1】:

任务的描述似乎与给定的输入和输出不匹配,但以下内容应该可以帮助您,因为它说明了您似乎缺少的部分 - 即结合两个操作,只需将它们组合成一个管道(即使用|)。

另一个关键点是建议将参数(例如本例中的$region)作为参数传递给jq程序。

program.jq

  .Statement += [ 
    {
     "Sid": "AllowForSpecificLambda",
     "Effect": "Allow",
     "Principal": {
        "AWS": [
            "arn:aws:iam::XXXXXXXXXX:role/lambda_allow_pretoken_generation"
        ]
       },
     "Action": "s3:Get*","Resource": [
            "arn:aws:s3:::test-XXXX-cognito-settings-" + $region

            ]}
        ]
  | .Statement[0].Condition.StringNotLike."aws:userId" += ["Hello"]

调用

假设您希望$region 具有某些价值,请说“地区”:

jq --arg region REGION -f program.jq test.json

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    相关资源
    最近更新 更多