Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 524

Using JQ query how to update propery inside two nested arrays?

$
0
0

I tried this for 2 days but can't figure it out in any way.

This is my JSON:

{"items": [        {"name":"itemA","bins":[                {"bin_id":"b1","count":"11"                },                {"bin_id":"b2","count":"22"                }            ]        },        {"name":"itemB","bins":[                {"bin_id":"b5","count":"55"                }            ]        }    ]}

Important note. Input JSON could be empty object '{}' or missing any part of the hierarchy.

I need to insert whole hierary or update final entry for "count". My input arguments are: itemName, binId and new count.

So for "itemA", "b1" and count 68. I need to set .items -> (item with name: "itemA") -> (bin with bin_id 68) -> count = 68.

I tried using INDEX to access items. And that works... but I can't seem to further access bins and update final count.

UPDATE:

I got something like this:

.items |= [INDEX(.[]?;.name) | .itemA |= .+ {name:"itemA",bins:[INDEX(.bins[]?;.bin_id) | .b1 |= .+ {bin_id:"b1",count:"188000"} | .[]]} | .[]]

But it does look ugly. Can this be done more clearly?


Viewing all articles
Browse latest Browse all 524

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>