后代修改后如何打印出顶层json
How to print out the top-level json after modification of descendants
您好,我成功创建了这个 jq 过滤器 .profiles | recurse | .gameDir? | if type == "null" then "" else . end | scan("{REPLACE}.*") | sub("{REPLACE}"; "{REPLACESTRINGHERE}")
。它成功地替换了我想要的(在 jqplay.org 处检查)但现在我想打印完整的 json 而不仅仅是修改后的字符串
正在调整您的查询:
.profiles |= walk( if type == "object" and has("gameDir")
then .gameDir |=
(if type == "null" then "" else . end
| scan("{REPLACE}.*") | sub("{REPLACE}"; "{REPLACESTRINGHERE}"))
else .
end )
(可以轻松调整以提高效率。)
如果你的 jq 没有 walk
,你可以 google 它(jq “def walk”)或者从 jq FAQ https://github.com/stedolan/jq/wiki/FAQ
walk-free接近
作为记录,这里有一个使用 paths
的 walk-free 方法的示例。以下内容还对替换字符串的计算进行了一些更改——特别是它消除了 scan
的使用——因此它在逻辑上不等价,但可能更有用也更有效。
.profiles |=
( . as $in
| reduce (paths | select(.[-1] == "gameDir")) as $path ($in;
($in | getpath($path)
| if type == "null" then ""
else sub(".*{REPLACE}"; "{REPLACESTRINGHERE}")
end) as $value
| setpath($path; $value) ))
您好,我成功创建了这个 jq 过滤器 .profiles | recurse | .gameDir? | if type == "null" then "" else . end | scan("{REPLACE}.*") | sub("{REPLACE}"; "{REPLACESTRINGHERE}")
。它成功地替换了我想要的(在 jqplay.org 处检查)但现在我想打印完整的 json 而不仅仅是修改后的字符串
正在调整您的查询:
.profiles |= walk( if type == "object" and has("gameDir")
then .gameDir |=
(if type == "null" then "" else . end
| scan("{REPLACE}.*") | sub("{REPLACE}"; "{REPLACESTRINGHERE}"))
else .
end )
(可以轻松调整以提高效率。)
如果你的 jq 没有 walk
,你可以 google 它(jq “def walk”)或者从 jq FAQ https://github.com/stedolan/jq/wiki/FAQ
walk-free接近
作为记录,这里有一个使用 paths
的 walk-free 方法的示例。以下内容还对替换字符串的计算进行了一些更改——特别是它消除了 scan
的使用——因此它在逻辑上不等价,但可能更有用也更有效。
.profiles |=
( . as $in
| reduce (paths | select(.[-1] == "gameDir")) as $path ($in;
($in | getpath($path)
| if type == "null" then ""
else sub(".*{REPLACE}"; "{REPLACESTRINGHERE}")
end) as $value
| setpath($path; $value) ))