How to copy from object with out using the table index

45 Views Asked by At

The input is

{
"zoo": [
   { "x": { "guesst": "" } },
   { "y": { "guesst": "zebra" } }
  ]
}

And i need the output like

{
"zoo": [
   { "x": { "guesst": "zebra" } },
   { "y": { "guesst": "zebra" } }
  ]
}

I did:

jq '.zoo[0].x.guesst = .zoo[1].y.guesst'

But, what if i do not know the order of the objects in the array?

1

There are 1 best solutions below

0
pmf On BEST ANSWER

Iterate over all items using .[], and use select and has to filter for the right ones:

jq '(.zoo[] | select(has("x"))).x.guesst = (.zoo[] | select(has("y"))).y.guesst'

Demo