简体   繁体   English

如何一次在jq中设置多个路径值?

[英]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 . 我已经弄清楚了如何使用setpath在我的JSON文件package.json中设置一个值。 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: 我想做的是使用如下所示的模式,因此它还将路径设置为“ acme-b”,“ acme-c”等:

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

Does jq support that, and if so, how is it accomplished? jq是否支持该功能,如果支持,如何实现?

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

One could also use 'startswith'. 也可以使用“ startswith”。 It might be appropriate to use 'walk'. 使用“行走”可能是适当的。

To use 'setpath', one could use 'reduce' (eg with 'paths'), eg: 要使用“ setpath”,可以使用“ reduce”(例如带有“ paths”),例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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