0

I have a json that looks like this:

{
    "failedSet": [],
    "successfulSet": [{
        "event": {
            "arn": "arn:aws:health:us-east-1::event/AWS_RDS_MAINTENANCE_SCHEDULED_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx",
            "endTime": 1502841540.0,
            "eventTypeCategory": "scheduledChange",
            "eventTypeCode": "AWS_RDS_MAINTENANCE_SCHEDULED",
            "lastUpdatedTime": 1501208541.93,
            "region": "us-east-1",
            "service": "RDS",
            "startTime": 1502236800.0,
            "statusCode": "open"
        },
        "eventDescription": {
            "latestDescription": "We are contacting you to inform you that one or more of your Amazon RDS DB instances is scheduled to receive system upgrades during your maintenance window between August 8 5:00 PM and August 15 4:59 PM PDT. Please see the affected resource tab for a list of these resources. \r\n\r\nWhile the system upgrades are in progress, Single-AZ deployments will be unavailable for a few minutes during your maintenance window. Multi-AZ deployments will be unavailable for the amount of time it takes a failover to complete, usually about 60 seconds, also in your maintenance window. \r\n\r\nPlease ensure the maintenance windows for your affected instances are set appropriately to minimize the impact of these system upgrades. \r\n\r\nIf you have any questions or concerns, contact the AWS Support Team. The team is available on the community forums and by contacting AWS Premium Support. \r\n\r\nhttp://aws.amazon.com/support\r\n"
        }
    }]
}

I'm trying to add a new key/value under successfulSet[].event (key name as affectedEntities) using jq, I've seen some examples, like here and here, but none of those answers really show how to add a possible one key with multiple values (the reason why I say possible is because as of now, AWS is returning one value for the affected entity, but if there are more, then I'd like to list them).

EDIT: The value of the new key that I want to add is stored in a variable called $affected_entities and a sample of that value looks like this:

[
    "arn:aws:acm:us-east-1:xxxxxxxxxxxxxx:certificate/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"
]

The value could look like this:

[
    "arn:aws:acm:us-east-1:xxxxxxxxxxxxxx:certificate/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx", 
    "arn:aws:acm:us-east-1:xxxxxxxxxxxxxx:certificate/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx",
    ...
    ...
    ...
]
Fadi
  • 1,187
  • 5
  • 17
  • 40

1 Answers1

2

You can use this jq,

jq '.successfulSet[].event += { "new_key" : "new_value" }' file.json

EDIT:

Try this:

jq --argjson argval "$new_value" '.successfulSet[].event += { "affected_entities" : $argval }' file.json

Test:

sat~$ new_value='[
    "arn:aws:acm:us-east-1:xxxxxxxxxxxxxx:certificate/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"
]'

sat~$ jq --argjson argval "$new_value" '.successfulSet[].event += { "affected_entities" : $argval }' file.json

Note that --argjson works with jq 1.5 and above.

Fadi
  • 1,187
  • 5
  • 17
  • 40
sat
  • 13,503
  • 5
  • 41
  • 62
  • Not working, I'm doing the following: `jq '.successfulSet[].event += { "affectedEntities" : "${affected_entities}" }' file.json` – Fadi Aug 11 '17 at 07:36
  • Ah, that works! This only works with jq 1.5 though, which isn't a big deal, I followed this https://stackoverflow.com/a/42622937/4557537 to update jq on Ubuntu 14.04. – Fadi Aug 11 '17 at 08:07