简体   繁体   中英

How do I set multiple path values in jq at once?

I have figured out how to set a value in my JSON file, package.json, using setpath . Can I do this using a pattern?

cat package.json | jq 'setpath(["dependencies", "acme-a"]; "mytagname")'

What I would like to do is use a pattern like the following so it also sets the paths at "acme-b", "acme-c", and so on:

cat package.json | jq 'setpath(["dependencies", "acme-*"]; "mytagname")'

Does jq support that, and if so, how is it accomplished?

.dependencies |= with_entries(
  if .key|test("^acme-") then .value = "mytagname" else . end )

One could also use 'startswith'. It might be appropriate to use 'walk'.

To use 'setpath', one could use 'reduce' (eg with 'paths'), eg:

reduce paths as $p (.;
  if $p[-1] | test("^acme-") then setpath($p; "mytagname") else . end)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM