Skip to content

Instantly share code, notes, and snippets.

@ambakshi
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ambakshi/505a2fb7911491cfa11f to your computer and use it in GitHub Desktop.
Save ambakshi/505a2fb7911491cfa11f to your computer and use it in GitHub Desktop.
Using jq to modify a Json object
// Modify the following JSON object such that the SnapshotId of device /dev/sda1 is 'snap-12345678' and size is 4,
// while maintaining the structure of the of the object.
// You can't make assumptions about how it is formatted, except to say it's legal json.
[
{
"Ebs": {
"VolumeSize": 32,
"SnapshotId": "snap-ee16314e",
"DeleteOnTermination": true
},
"DeviceName": "/dev/sda1"
},
{
"VirtualName": "ephemeral0",
"DeviceName": "/dev/sdb"
},
{
"VirtualName": "ephemeral1",
"DeviceName": "/dev/sdc"
},
{
"Ebs": {
"VolumeSize": 500,
"SnapshotId": "snap-e5133445",
"DeleteOnTermination": false
},
"DeviceName": "/dev/sdf"
}
]
// You want this:
[
{
"Ebs": {
"VolumeSize": 4,
"SnapshotId": "snap-12345678",
"DeleteOnTermination": true
},
"DeviceName": "/dev/sda1"
},
{
"VirtualName": "ephemeral0",
"DeviceName": "/dev/sdb"
},
{
"VirtualName": "ephemeral1",
"DeviceName": "/dev/sdc"
},
{
"Ebs": {
"VolumeSize": 500,
"SnapshotId": "snap-e5133445",
"DeleteOnTermination": false
},
"DeviceName": "/dev/sdf"
}
]
$ cat conf/jobs-jenkins-master-bdm.json | jq '.[0].Ebs.VolumeSize = 4' | jq '.[0].Ebs.SnapshotId = "snap-12345678"'
````
[
{
"DeviceName": "/dev/sda1",
"Ebs": {
"DeleteOnTermination": true,
"SnapshotId": "snap-12345678",
"VolumeSize": 4
}
},
{
"DeviceName": "/dev/sdb",
"VirtualName": "ephemeral0"
},
{
"DeviceName": "/dev/sdc",
"VirtualName": "ephemeral1"
},
{
"DeviceName": "/dev/sdf",
"Ebs": {
"DeleteOnTermination": false,
"SnapshotId": "snap-e5133445",
"VolumeSize": 500
}
}
]
````
$ jq '[.[]|(select(.DeviceName == "/dev/sda1").Ebs.SnapshotId = "snap-12345678")|( select(.DeviceName == "/dev/sda1").Ebs.VolumeSize = 4) // .]' block-device-map.json
```
[
{
"Ebs": {
"VolumeSize": 4,
"SnapshotId": "snap-12345678",
"DeleteOnTermination": true
},
"DeviceName": "/dev/sda1"
},
{
"VirtualName": "ephemeral0",
"DeviceName": "/dev/sdb"
},
{
"VirtualName": "ephemeral1",
"DeviceName": "/dev/sdc"
},
{
"Ebs": {
"VolumeSize": 500,
"SnapshotId": "snap-e5133445",
"DeleteOnTermination": false
},
"DeviceName": "/dev/sdf"
}
]
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment