如何一次在 jq 中设置多个路径值?
How do I set multiple path values in jq at once?
我已经知道如何使用 setpath
在我的 JSON 文件 package.json 中设置一个值。我可以使用模式来做到这一点吗?
cat package.json | jq 'setpath(["dependencies", "acme-a"]; "mytagname")'
我想做的是使用类似下面的模式,因此它还将路径设置为 "acme-b"、"acme-c",依此类推:
cat package.json | jq 'setpath(["dependencies", "acme-*"]; "mytagname")'
jq 支持吗?如果支持,它是如何实现的?
.dependencies |= with_entries(
if .key|test("^acme-") then .value = "mytagname" else . end )
也可以使用 'startswith'。使用 'walk'.
可能比较合适
要使用 'setpath',可以使用 'reduce'(例如使用 'paths'),例如:
reduce paths as $p (.;
if $p[-1] | test("^acme-") then setpath($p; "mytagname") else . end)
我已经知道如何使用 setpath
在我的 JSON 文件 package.json 中设置一个值。我可以使用模式来做到这一点吗?
cat package.json | jq 'setpath(["dependencies", "acme-a"]; "mytagname")'
我想做的是使用类似下面的模式,因此它还将路径设置为 "acme-b"、"acme-c",依此类推:
cat package.json | jq 'setpath(["dependencies", "acme-*"]; "mytagname")'
jq 支持吗?如果支持,它是如何实现的?
.dependencies |= with_entries(
if .key|test("^acme-") then .value = "mytagname" else . end )
也可以使用 'startswith'。使用 'walk'.
可能比较合适要使用 'setpath',可以使用 'reduce'(例如使用 'paths'),例如:
reduce paths as $p (.;
if $p[-1] | test("^acme-") then setpath($p; "mytagname") else . end)