Reputation: 13
I'm new to jq
and try to extract the values of the images
keys into a new key, so that
{
"environments": {
"staging": {
"apps": {
"web": {
"image": "image1"
}
}
},
"production": {
"apps": {
"web": {
"image": "image2"
},
"admin": {
"image": "image3"
}
}
}
}
}
becomes something like this:
{
"environments": {
"staging": {
"images": [
"image1"
]
}
},
"production": {
"images": [
"image2",
"image3"
]
}
}
I've been fiddling around with jq
for a while now, could anyone give me a hint?
Upvotes: 1
Views: 217
Reputation: 13239
Assuming that in the result you expect the environments
object to include both staging
and production
, you could use this jq
command:
<file jq '.environments |= (.[] |= {images : (.apps|map(.[]))})'
{
"environments": {
"staging": {
"images": [
"image1"
]
},
"production": {
"images": [
"image2",
"image3"
]
}
}
}
This replaces the content of the environments
object with the a new inner images
object that contains the list images referred by apps
and image
.
The array of images is built using the jq builtin map()
function.
Upvotes: 1