Jayesh Dhandha
Jayesh Dhandha

Reputation: 2119

Add and modify JSON object using jq

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

Upvotes: 0

Views: 488

Answers (1)

peak
peak

Reputation: 116690

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

Upvotes: 1

Related Questions