根据 JSON 中的嵌套标签获取所有嵌套 url

发布于 2025-01-20 00:32:51 字数 2159 浏览 0 评论 0原文

我在文本文件 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_carrotoptions 列表中的第一个 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_carrotcat_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

π浅易 2025-01-27 00:32:51

如果要串联两个阵列,则可以使用+操作员:

jq -c '[..|.animal_potato_carrot?|select(. != null)|.options[0].url] + [..|.cat_dog_rabbit?|select(. != null)|.[0].url]' json.txt

请注意,结果中项目的顺序并不完全如您的要求,因为首先是所有Animal_potato_carrot -确定URL,然后所有cat_dog_rabbit -urls。

将两个过滤器与相结合,可能最接近您的需求:

jq -c '[..|(.animal_potato_carrot?.options),(.cat_dog_rabbit?)|.[0].url|select(. != null)]' json.txt

If you want to concatenate two arrays you can use the + operator:

jq -c '[..|.animal_potato_carrot?|select(. != null)|.options[0].url] + [..|.cat_dog_rabbit?|select(. != null)|.[0].url]' json.txt

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 all cat_dog_rabbit-urls.

Combining two filters with , may come closest to your needs:

jq -c '[..|(.animal_potato_carrot?.options),(.cat_dog_rabbit?)|.[0].url|select(. != null)]' json.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文