0

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

4
  • 1
    it would be better if you posted the initial json data and the final expected result Commented Apr 3, 2018 at 12:49
  • 1
    Why tag this with sed when you want to do this two things in single command and the command you're using is jq? Commented Apr 3, 2018 at 14:30
  • The given desired output does not have anything with "Test" or "Hello" in it, so it's unclear where the "new value" is supposed to go. Commented Apr 3, 2018 at 15:06
  • @peak I have added the desired "Hello" that should be appended in output. Thanks! Commented Apr 4, 2018 at 5:38

1 Answer 1

1

The description of the task does not seem to match the given input and output, but the following should get you on your way, as it illustrates the piece you seem to be missing -- that is, to combine the two operations, simply combine them into a pipeline (i.e., using |).

Another key point is that it is advisable to pass in parameters (such as $region in the present case) as arguments to the jq program.

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

Invocation

Assuming you want $region to have some value, say "REGION":

jq --arg region REGION -f program.jq test.json
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.