I'm describing the output I'd like to have using jq
Here my json file
{
"partitions": [
{
"replicas": [
0,
1,
2
],
"log_dirs": [
"any",
"any",
"any"
]
},
{
"replicas": [
2,
0,
1
],
"log_dirs": [
"any",
"any",
"any"
]
},
[...]
I would like, for every object in partitions[] to replace the value of the i th string in log_dirs[] by its concatenation with the i th number in replicas[] in order to have something like this
{
"partitions": [
{
"replicas": [
0,
1,
2
],
"log_dirs": [
"any0",
"any1",
"any2"
]
},
{
"replicas": [
2,
0,
1
],
"log_dirs": [
"any2",
"any0",
"any1"
]
},
[...]
CodePudding user response:
Use a reduce
loop with a range
.partitions[] |= reduce range(0; ( .replicas | length ) ) as $r
( . ; .log_dirs[$r] = ( .replicas[$r] | tostring ) )
The reduce expression works by iterating over the entire log_dirs
array upto the length of replicas
list and modifying each entry .log_dirs[$r]
(here r
runs from 0 - length of replicas
) by appending the corresponding value at replicas[$r]
. Since replicas
contains numerics, it needs to be converted to string for the append operation.
CodePudding user response:
I'm not quite happy with this, but it does work:
jq '.partitions[] |= . (
. as $p | {
log_dirs: [
range(.replicas | length) |
"\($p.log_dirs[.])\($p.replicas[.])"
]
}
)' in.json