给出任意 JSON输入:
{
"id":"038020",
"title":"Teenage Mutant Ninja Turtles: Out of the Shadows",
"turtles":[
{
"name":"Leonardo",
"mask":"blue"
},
{
"name":"Michelangelo",
"mask":"orange"
},
{
"name":"Donatello",
"mask":"purple"
},
{
"name":"Raphael",
"mask":"red"
}
],
"summary":"The Turtles continue to live in the shadows and no one knows they were the ones who took down Shredder",
"cast":"Megan Fox, Will Arnett, Tyler Perry",
"director":"Dave Green"
}
一个任意 JQ路径列表,例如[".turtles[].name", ".cast", ".does.not.exist"]
或任何类似格式
如何仅使用列表路径中包含的信息创建新的JSON? 在这种情况下,预期结果将是:
{
"turtles":[
{
"name":"Leonardo"
},
{
"name":"Michelangelo"
},
{
"name":"Donatello"
},
{
"name":"Raphael"
}
],
"cast":"Megan Fox, Will Arnett, Tyler Perry"
}
我在使用"removing null
entries"的JSON中看到了walk function present in jq1.5+等问题中的类似解决方案,有点像以下几行:
def filter_list(input, list):
input
| walk(
if type == "object" then
with_entries( select(.key | IN( list )))
else
.
end);
filter_list([.], [.a, .b, .c[].d])
但它应该以某种方式考虑JSON中的完整路径。
解决此问题的最佳方法是什么?
答案 0 :(得分:2)
如果$ paths包含一个显式jq路径数组(例如[ ["turtles", 0, "name"], ["cast"]])
),最简单的方法是
使用以下过滤器:
. as $in
| reduce $paths[] as $p (null; setpath($p; $in | getpath($p)))
为了能够处理扩展路径表达式,例如[“turtles”,[],“name”],其中[]
的范围超出turtles
数组的索引,我们将定义以下辅助函数:
def xpath($ary):
. as $in
| if ($ary|length) == 0 then null
else $ary[0] as $k
| if $k == []
then range(0;length) as $i | $in[$i] | xpath($ary[1:]) | [$i] + .
else .[$k] | xpath($ary[1:]) | [$k] + .
end
end ;
为了说明,我们还要定义:
def paths($ary): $ary[] as $path | xpath($path);
然后使用给定的输入,表达式:
. as $in
| reduce paths([ ["turtles", [], "name"], ["cast"]]) as $p
(null; setpath($p; $in | getpath($p)) )
产生如下所示的输出。
path
值得指出的是,处理诸如“.turtles [] .name”之类的表达式的一种方法是使用内置过滤器path/1
。
例如:
# Emit a stream of paths:
def paths: path(.turtles[].name), ["cast"];
. as $in
| reduce paths as $p (null; setpath($p; $in | getpath($p)))
{
"turtles": [
{
"name": "Leonardo"
},
{
"name": "Michelangelo"
},
{
"name": "Donatello"
},
{
"name": "Raphael"
}
],
"cast": "Megan Fox, Will Arnett, Tyler Perry"
}