1

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?

peak
  • 105,803
  • 17
  • 152
  • 177
Stewart
  • 1,659
  • 4
  • 23
  • 35
  • What are the contents of `package.json`, need it to reproduce it locally – Inian Mar 27 '18 at 14:03
  • @Inian For the purposes of this, let's say it looks like `{"dependencies": {"acme-a": "1.0.0", "acme-b": "2.0.0", "acme-c": "3.0.0"} }` – Stewart Mar 27 '18 at 14:16

1 Answers1

3
.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' (e.g. with 'paths'), e.g.:

reduce paths as $p (.;
  if $p[-1] | test("^acme-") then setpath($p; "mytagname") else . end)
peak
  • 105,803
  • 17
  • 152
  • 177