0

Need some help for change the value in JSON file using jq command.

Error:

[jenkins@devops-dev-02 New]$ jq 'map(if .Tags[11].Key == "Period" then .Tags[11].Value = "Weekly" else . end)' create_snapshot.json
jq: error (at create_snapshot.json:54): Cannot index string with string "Tags"
[jenkins@devops-dev-02 New]$

JSON File:

{
   "DBSnapshotIdentifier":"snapshot-myrds-backup-1",
   "DBInstanceIdentifier":"myrds",
   "Tags":[
      {
         "Key":"Name",
         "Value":"snapshot-myrds-backup-1"
      },
      {
         "Key":"Application",
         "Value":"myapp"
      },
      {
         "Key":"Environment",
         "Value":"Dev and QA"
      },
      {
         "Key":"Period",
         "Value":"Daily"
      }
   ]
}
1
  • map() doesn't make sense as the top-level call when your only item is a dict. Commented Aug 29, 2021 at 4:00

1 Answer 1

2

When you call map() over a dictionary, you operate on only its values, throwing the keys away. The value "snapshot-myrds-backup-1" has no .Tags, nor can you take item 11 of that nonexistent Tags entry.


What you want here is:

.Tags |= map(if .Key == "Period" then .Value = "Weekly" else . end)

map() is usually used to iterate over an array; for JSON objects, map_values() would usually be used to "map" over the object's values while retaining the keys, except for those whose values are mapped to empty.

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.