Sample JSON Input:
{
"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"
]
}
}
}
]
}
Expected JSON Output:
{
"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"
]
}
}
]
}
Pardon me i have done some syntax mistake in the json tags. All i want is inside my statement array object i want to add new object + modify existing object. I am adding new JSON object using jq. Below is my code snippet which is working fine.
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
I am addin new value in my JSON array using below code snippet.
jq '.Statement[]
| select(.Sid == "Test")
.Condition.StringNotLike."aws:userId"[.Condition.StringNotLike."aws:userId"| length]
|= . + "Hello"' test.json
How can i do this two things in single command?
Thanks
sedwhen you want todo this two things in single commandand the command you're using isjq?