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

Using JQ how to append entry to dictionary type array in JSON or create it if array doesn't exist?

$
0
0

I have ideal JSON:

{"environments":[        {"name":"test","release":"2.0"        },        {"name":"prod","release":"1.0"        },    ]}

This file could be in any state:

  1. It can be empty file with just empty object { }
  2. It can have empty "environemnts" array
  3. It can have some entries in environment array but not the one we want to update

I need to update or create the "release" tag with given version under given environment.

I tried variations of:

(.environments[] | select(.name == "prod") | .release) = "3.0" // .environments += [{"name": "prod", "release": "3.0"}]

I also tried:

.environments|=  (map(.name) | index("prod")) as $ix    | if $ix       then .[$ix]["release"] = "2.0"       else . + [{name: "release", value: "2.0"}]      end

But it either fails on empty file. Or doesn't create new entry.I'm lost here.


Viewing all articles
Browse latest Browse all 524

Trending Articles



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