jsonjqedit

How to copy from object with out using the table index


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?


Solution

  • 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