示例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对象。下面是我的代码片段工作正常。
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
我使用下面的代码片段在我的JSON数组中添加新值。
jq '.Statement[]
| select(.Sid == "Test")
.Condition.StringNotLike."aws:userId"[.Condition.StringNotLike."aws:userId"| length]
|= . + "Hello"' test.json
我如何在单一命令中执行这两项操作?
由于
答案 0 :(得分:1)
任务的描述似乎与给定的输入和输出不匹配,但是下面的内容应该会让你上路,因为它说明了你似乎缺少的部分 - 也就是说,要结合这两个操作,只需将它们组合成一个管道(即使用|
)。
另一个关键点是建议将参数(例如本例中的$region
)作为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
有一些价值,请说“REGION”:
jq --arg region REGION -f program.jq test.json