根据 JSON 中的嵌套标签获取所有嵌套 url
我在文本文件 json.txt
中有以下 Json 输入:
{
"files":[
{
"id":49894894,
"list":[
{
"name":"one",
"animal_potato_carrot":{
"options":[
{
"id":4989,
"url":"https://example.com/text.txt"
},
{
"id":3994,
"url":"https://example.com/randomfile.json"
}
]
}
},
{
"name":"two",
"cat_dog_rabbit":[
{
"id":4989,
"url":"https://example.com/text2.txt"
},
{
"id":3994,
"url":"https://example.com/randomfile.json"
}
]
},
{
"name":"three",
"animal_potato_carrot":{
"options":[
{
"id":4989,
"url":"https://example.com/text3.txt"
},
{
"id":3994,
"url":"https://example.com/randomfile.json"
}
]
}
}
]
}
]
}
我只想获取每个 animal_potato_carrot
的 options
列表中的第一个 url > 或 cat_dog_rabbit
仅嵌套标签(注意它们具有不同的结构)
所以我的输出将是这些块中的前三个网址:
["https://example.com/text.txt", "https://example.com/text2.txt, "https://example.com/text3.txt"]
我尝试了 jq json.txt -c '.. |.“动物土豆胡萝卜”? | select(. != null)'
但这会返回正文中的所有内容,而不仅仅是第一个网址。
编辑:
这两个命令分别返回 animal_potato_carrot
和 cat_dog_rabbit
的 url,但是有没有办法组合这些命令?
jq -c '[..|.animal_potato_carrot?|select(. != null)|.options[0].url]' json.txt
jq -c '[..|.cat_dog_rabbit?|select(. != null)|.[0].url]' json.txt
I have a following Json input in a text file json.txt
:
{
"files":[
{
"id":49894894,
"list":[
{
"name":"one",
"animal_potato_carrot":{
"options":[
{
"id":4989,
"url":"https://example.com/text.txt"
},
{
"id":3994,
"url":"https://example.com/randomfile.json"
}
]
}
},
{
"name":"two",
"cat_dog_rabbit":[
{
"id":4989,
"url":"https://example.com/text2.txt"
},
{
"id":3994,
"url":"https://example.com/randomfile.json"
}
]
},
{
"name":"three",
"animal_potato_carrot":{
"options":[
{
"id":4989,
"url":"https://example.com/text3.txt"
},
{
"id":3994,
"url":"https://example.com/randomfile.json"
}
]
}
}
]
}
]
}
I want to get only the first url in the list of options
for each animal_potato_carrot
or cat_dog_rabbit
nested tag only (note they have different structures)
So my output will be first three urls in those blocks:
["https://example.com/text.txt", "https://example.com/text2.txt, "https://example.com/text3.txt"]
I tried jq json.txt -c '.. |."animal_potato_carrot"? | select(. != null)'
but that returns all the things inside the body, not just the FIRST url.
Edit:
these two commands return the urls for animal_potato_carrot
and cat_dog_rabbit
separately but is there a way to combine these commands?
jq -c '[..|.animal_potato_carrot?|select(. != null)|.options[0].url]' json.txt
jq -c '[..|.cat_dog_rabbit?|select(. != null)|.[0].url]' json.txt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要串联两个阵列,则可以使用
+
操作员:请注意,结果中项目的顺序并不完全如您的要求,因为首先是所有
Animal_potato_carrot
-确定URL,然后所有cat_dog_rabbit
-urls。将两个过滤器与
相结合,
可能最接近您的需求:If you want to concatenate two arrays you can use the
+
operator:Please notice that the order of items in the result is not exactly as you requested, because first all
animal_potato_carrot
-urls are determined and then allcat_dog_rabbit
-urls.Combining two filters with
,
may come closest to your needs: