简体   繁体   English

使用 jq 从 JSON 中的嵌套数组中选择 jq 给出空结果

[英]Selecting from nested array in JSON using jq gives empty result

I've been racking my brains over this because on its face, the jq statement I'm using should produce the desired result, but using a tool like jqplay to test gives me a blank result....我一直在为此绞尽脑汁,因为从表面上看,我使用的 jq 语句应该会产生预期的结果,但是使用 jqplay 之类的工具进行测试却给了我一个空白的结果......

Here is the JSON I'm using:这是我正在使用的 JSON:

{
  "DBInventory":
    {
      "InventoryID": "ABSCBD",
      "Inventory":[
            {
              "Sys1": 
                {
                  "folder":[
                    {
                      "Scripts":
                        {
                          "files": [
                            "FileName1O",
                            "FileName2O"
                            ]
                        }
                        
                    },
                    {
                      "Schemas":
                        {
                          "files": [
                            "FileName1S",
                            "FileName2S"
                            ]
                        }
                                   
                    },
                    {
                      "StoredProcedures":
                        {
                          "files": [
                            "FileName1SP",
                            "FileName2SP",
                            "FileName3SP"
                            ]
                        }
                            
                    },
                    {
                      "Tables":
                        {
                          "files": [
                            "FileName1T",
                            "FileName2T"
                            ]
                        }
                                      
                    }
                    ]
                }
                
           
            },
            {
              "Sys2": 
                {
                  "folder":[
                    {
                      "MaintScripts":
                        {
                          "files": [
                            "FileName1",
                            "FileName2"
                            ]
                        }
                        
                    },
                    {
                      "Schemas":
                        {
                          "files": [
                            "FileName1",
                            "FileName2"
                            ]
                        }
                                 
                    },
                    {
                      "StoredProcedures":
                        {
                          "files": [
                            "FileName1",
                            "FileName2"
                            ]
                        }
                              
                    },
                    {
                      "Tables":
                        {
                          "files": [
                            "FileName1",
                            "FileName2"
                            ]
                        }
                                        
                    },
                    {
                      "ExternalTables":
                        {
                          "files": [
                            "FileName1",
                            "FileName2"
                            ]
                        }
                              
                    }, 
                    {
                      "Scripts":
                        {
                          "files": [
                            "FileName1",
                            "FileName2"
                            ]
                        }
                                       
                    },
                    {
                      "Views":
                        {
                          "files": [
                            "FileName1v",
                            "FileName2v"
                            ]
                        }
                                       
                    }
                    ]
                }
            }
        ]
    }
  
}

I'm trying to select an object only return the filenames in the array under it like so (say I wanted just the files in 'Schemas' for 'Sys1').我正在尝试 select 和 object 只返回它下面的数组中的文件名(比如我只想要 'Sys1' 的 'Schemas' 中的文件)。 The desired output would be:所需的 output 将是:

["Filename1S","Filename2S"]

from this section:从本节开始:

Inventory > Sys1 > folder > Schemas

The jq statement I've been invoking is:我一直在调用的 jq 语句是:

.DBInventory.Inventory[0][] | select(any(.folder[]; .Key == "Schemas")) | .files[]

Any help would be greatly appreciated.任何帮助将不胜感激。 I think I'm close, but the whole blank output has thrown me for a loop.我想我已经接近了,但是整个空白 output 让我陷入了困境。

You don't need a select() at all here.您在这里根本不需要select() Consider:考虑:

jq '.DBInventory.Inventory[0][].folder[].Schemas?.files // empty'

The JSON data is not completely regular, so you could specify the path of interest more precisely, or sprinkle some "bailouts" (such as // empty or ? ) into the query, eg: JSON 数据不是完全规则的,因此您可以更精确地指定感兴趣的路径,或者在查询中添加一些“救助”(例如// empty? ),例如:

.DBInventory.Inventory[].Sys1 // empty
| (.folder[].Schemas // empty).files

or simply:或者简单地说:

try (.DBInventory.Inventory[].Sys1.folder[].Schemas.files) // empty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM