Reputation: 25
I have the following json doc (file.json):
[{
"mount": "/",
"size": "45G",
"usedperc": "26"
},
{
"mount": "/opt",
"size": "197G",
"usedperc": "20"
},
{
"mount": "/tmp",
"size": "5.0G",
"usedperc": "8"
}]
Given this doc , I would like to transform into a different doc, with this structure:
[
{
"key": "val1",
"mnt": "/-rootFS",
"key3": {
"key4": "val4"
}
},
{
"key": "val1",
"mnt": "/opt-optFS",
"key3": {
"key4": "val4"
}
},
{
"key": "val1",
"mnt": "/tmp-tmpFS",
"key3": {
"key4": "val4"
}
}
]
HOwever, when trying to apply the transformation with js, it stops on the first iteration of the document , and does not process the rest of the doc:
jq ' [{
"key":"val1",
"mnt": .[]|(if select(.mount == "/") then "/-rootFS" elif select(.mount == "/opt") then "/opt-optFS" elif select(.mount == "/tmp") then "/tmp-tmpFS" else . end ) ,
"key3":{
"key4": "val4"
}}]' < file.json
#output
[
{
"key": "val1",
"mnt": "/-rootFS",
"key3": {
"key4": "val4"
}
}
]
Not sure how to do it , I have tried in many different ways, mostly getting a "Cannot index array with string "mount"" error. Btw this is actually just a test case from a bigger jq expression, so I would like to do it with jq if that is possible
Upvotes: 0
Views: 43
Reputation: 117017
Here's one way:
map( (.mount
| if . == "/" then "root"
elif . == "/opt" then "opt"
else "tmp" end) as $type
| {key: "val1",
mnt: "\(.mount)-\($type)FS",
key3: {key4: "val4"}} )
Upvotes: 1