热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用jq提取可变数量的字段-Extractingvariablenumberoffieldsusingjq

IamnewtothiswebsiteandcameherebecauseIamreallystrugglingwithaproblemofextractingi

I am new to this website and came here because I am really struggling with a problem of extracting information from a JSON file. The tricky part is that there are variable number of fields, so I can't get away with simple syntax.

我是这个网站的新手来到这里,因为我真的很难解决从JSON文件中提取信息的问题。棘手的部分是存在可变数量的字段,因此我无法使用简单的语法。

Here's a sample code:

这是一个示例代码:

{
  "addresses": {
    "@count": "1",
    "address_name": {
      "address_spec": {
        "@addr_no": "1",
        "full_address": "Tel Aviv Univ, Eitan Berglas Sch Econ, IL-69978 Tel Aviv, Israel",
        "organizations": {
          "@count": "2",
          "organization": [
            "Tel Aviv Univ",
            {
              "@pref": "Y",
              "#text": "Tel Aviv University"
            }
          ]
        },
        "suborganizations": {
          "@count": "1",
          "suborganization": "Eitan Berglas Sch Econ"
        },
        "city": "Tel Aviv",
        "country": "Israel",
        "zip": {
          "@location": "BC",
          "#text": "IL-69978"
        }
      }
    }
  },
  "category_info": {
    "headings": {
      "@count": "1",
      "heading": "Social Sciences"
    },
    "subjects": {
      "@count": "3",
      "subject": [
        {
          "@ascatype": "traditional",
          "#text": "Economics"
        },
        {
          "@ascatype": "extended",
          "#text": "Business & Economics"
        },
        {
          "@ascatype": "traditional",
          "#text": "ECONOMICS"
        }
      ]
    }
  }
}
{
  "addresses": {
    "@count": "1",
    "address_name": {
      "address_spec": {
        "@addr_no": "1",
        "full_address": "MIT, Cambridge, MA 02139 USA",
        "organizations": {
          "@count": "2",
          "organization": [
            "MIT",
            {
              "@pref": "Y",
              "#text": "Massachusetts Institute of Technology (MIT)"
            }
          ]
        },
        "city": "Cambridge",
        "state": "MA",
        "country": "USA",
        "zip": {
          "@location": "AP",
          "#text": "02139"
        }
      }
    }
  },
  "category_info": {
    "headings": {
      "@count": "1",
      "heading": "Social Sciences"
    },
    "subjects": {
      "@count": "3",
      "subject": [
        {
          "@ascatype": "traditional",
          "#text": "Economics"
        },
        {
          "@ascatype": "extended",
          "#text": "Business & Economics"
        },
        {
          "@ascatype": "traditional",
          "#text": "ECONOMICS"
        }
      ]
    }
  }
}
{
  "addresses": {
    "@count": "2",
    "address_name": [
      {
        "address_spec": {
          "@addr_no": "1",
          "full_address": "Univ Kentucky, Lexington, KY 40506 USA",
          "organizations": {
            "@count": "2",
            "organization": [
              "Univ Kentucky",
              {
                "@pref": "Y",
                "#text": "University of Kentucky"
              }
            ]
          },
          "city": "Lexington",
          "state": "KY",
          "country": "USA",
          "zip": {
            "@location": "AP",
            "#text": "40506"
          }
        }
      },
      {
        "address_spec": {
          "@addr_no": "2",
          "full_address": "Univ Bonn, ZEI, D-5300 Bonn, Germany",
          "organizations": {
            "@count": "2",
            "organization": [
              "Univ Bonn",
              {
                "@pref": "Y",
                "#text": "University of Bonn"
              }
            ]
          },
          "suborganizations": {
            "@count": "1",
            "suborganization": "ZEI"
          },
          "city": "Bonn",
          "country": "Germany",
          "zip": {
            "@location": "BC",
            "#text": "D-5300"
          }
        }
      }
    ]
  },
  "category_info": {
    "headings": {
      "@count": "1",
      "heading": "Social Sciences"
    },
    "subjects": {
      "@count": "3",
      "subject": [
        {
          "@ascatype": "traditional",
          "#text": "Economics"
        },
        {
          "@ascatype": "extended",
          "#text": "Business & Economics"
        },
        {
          "@ascatype": "traditional",
          "#text": "ECONOMICS"
        }
      ]
    }
  }
}
{
  "addresses": {
    "@count": "1",
    "address_name": {
      "address_spec": {
        "@addr_no": "1",
        "full_address": "Harvard Univ, Cambridge, MA 02138 USA",
        "organizations": {
          "@count": "2",
          "organization": [
            "Harvard Univ",
            {
              "@pref": "Y",
              "#text": "Harvard University"
            }
          ]
        },
        "city": "Cambridge",
        "state": "MA",
        "country": "USA",
        "zip": {
          "@location": "AP",
          "#text": "02138"
        }
      }
    }
  },
  "category_info": {
    "headings": {
      "@count": "1",
      "heading": "Social Sciences"
    },
    "subjects": {
      "@count": "3",
      "subject": [
        {
          "@ascatype": "traditional",
          "#text": "Economics"
        },
        {
          "@ascatype": "extended",
          "#text": "Business & Economics"
        },
        {
          "@ascatype": "traditional",
          "#text": "ECONOMICS"
        }
      ]
    }
  }
}
{
  "addresses": {
    "@count": "3",
    "address_name": [
      {
        "address_spec": {
          "@addr_no": "1",
          "full_address": "Columbia Univ, New York, NY 10027 USA",
          "organizations": {
            "@count": "2",
            "organization": [
              "Columbia Univ",
              {
                "@pref": "Y",
                "#text": "Columbia University"
              }
            ]
          },
          "city": "New York",
          "state": "NY",
          "country": "USA",
          "zip": {
            "@location": "AP",
            "#text": "10027"
          }
        }
      },
      {
        "address_spec": {
          "@addr_no": "2",
          "full_address": "NYU, New York, NY USA",
          "organizations": {
            "@count": "2",
            "organization": [
              "NYU",
              {
                "@pref": "Y",
                "#text": "New York University"
              }
            ]
          },
          "city": "New York",
          "state": "NY",
          "country": "USA"
        }
      },
      {
        "address_spec": {
          "@addr_no": "3",
          "full_address": "Univ Pompeu Fabra, Barcelona, Spain",
          "organizations": {
            "@count": "2",
            "organization": [
              "Univ Pompeu Fabra",
              {
                "@pref": "Y",
                "#text": "Pompeu Fabra University"
              }
            ]
          },
          "city": "Barcelona",
          "country": "Spain"
        }
      }
    ]
  },
  "category_info": {
    "headings": {
      "@count": "1",
      "heading": "Social Sciences"
    },
    "subjects": {
      "@count": "3",
      "subject": [
        {
          "@ascatype": "traditional",
          "#text": "Economics"
        },
        {
          "@ascatype": "extended",
          "#text": "Business & Economics"
        },
        {
          "@ascatype": "traditional",
          "#text": "ECONOMICS"
        }
      ]
    }
  }
}
{
  "addresses": {
    "@count": "2",
    "address_name": [
      {
        "address_spec": {
          "@addr_no": "1",
          "full_address": "Univ Chicago, Chicago, IL 60637 USA",
          "organizations": {
            "@count": "2",
            "organization": [
              "Univ Chicago",
              {
                "@pref": "Y",
                "#text": "University of Chicago"
              }
            ]
          },
          "city": "Chicago",
          "state": "IL",
          "country": "USA",
          "zip": {
            "@location": "AP",
            "#text": "60637"
          }
        }
      },
      {
        "address_spec": {
          "@addr_no": "2",
          "full_address": "Amer Bar Fdn, Chicago, IL 60611 USA",
          "organizations": {
            "@count": "1",
            "organization": "Amer Bar Fdn"
          },
          "city": "Chicago",
          "state": "IL",
          "country": "USA",
          "zip": {
            "@location": "AP",
            "#text": "60611"
          }
        }
      }
    ]
  },
  "category_info": {
    "headings": {
      "@count": "1",
      "heading": "Social Sciences"
    },
    "subjects": {
      "@count": "3",
      "subject": [
        {
          "@ascatype": "traditional",
          "#text": "Economics"
        },
        {
          "@ascatype": "extended",
          "#text": "Business & Economics"
        },
        {
          "@ascatype": "traditional",
          "#text": "ECONOMICS"
        }
      ]
    }
  }
}
{
  "addresses": {
    "@count": "2",
    "address_name": [
      {
        "address_spec": {
          "@addr_no": "1",
          "full_address": "Ohio State Univ, Columbus, OH 43210 USA",
          "organizations": {
            "@count": "2",
            "organization": [
              "Ohio State Univ",
              {
                "@pref": "Y",
                "#text": "Ohio State University"
              }
            ]
          },
          "city": "Columbus",
          "state": "OH",
          "country": "USA",
          "zip": {
            "@location": "AP",
            "#text": "43210"
          }
        }
      },
      {
        "address_spec": {
          "@addr_no": "2",
          "full_address": "Harvard Univ, Cambridge, MA 02138 USA",
          "organizations": {
            "@count": "2",
            "organization": [
              "Harvard Univ",
              {
                "@pref": "Y",
                "#text": "Harvard University"
              }
            ]
          },
          "city": "Cambridge",
          "state": "MA",
          "country": "USA",
          "zip": {
            "@location": "AP",
            "#text": "02138"
          }
        }
      }
    ]
  },
  "category_info": {
    "headings": {
      "@count": "1",
      "heading": "Social Sciences"
    },
    "subjects": {
      "@count": "3",
      "subject": [
        {
          "@ascatype": "traditional",
          "#text": "Economics"
        },
        {
          "@ascatype": "extended",
          "#text": "Business & Economics"
        },
        {
          "@ascatype": "traditional",
          "#text": "ECONOMICS"
        }
      ]
    }
  }
}
{
  "addresses": {
    "@count": "1",
    "address_name": {
      "address_spec": {
        "@addr_no": "1",
        "full_address": "Univ Chicago, Chicago, IL 60637 USA",
        "organizations": {
          "@count": "2",
          "organization": [
            "Univ Chicago",
            {
              "@pref": "Y",
              "#text": "University of Chicago"
            }
          ]
        },
        "city": "Chicago",
        "state": "IL",
        "country": "USA",
        "zip": {
          "@location": "AP",
          "#text": "60637"
        }
      }
    }
  },
  "category_info": {
    "headings": {
      "@count": "1",
      "heading": "Social Sciences"
    },
    "subjects": {
      "@count": "3",
      "subject": [
        {
          "@ascatype": "traditional",
          "#text": "Economics"
        },
        {
          "@ascatype": "extended",
          "#text": "Business & Economics"
        },
        {
          "@ascatype": "traditional",
          "#text": "ECONOMICS"
        }
      ]
    }
  }
}
{
  "addresses": {
    "@count": "2",
    "address_name": [
      {
        "address_spec": {
          "@addr_no": "1",
          "full_address": "Wissensch Zentrum Berlin Sozialforsch, D-1000 Berlin, Germany",
          "organizations": {
            "@count": "1",
            "organization": "Wissensch Zentrum Berlin Sozialforsch"
          },
          "city": "Berlin",
          "country": "Germany",
          "zip": {
            "@location": "BC",
            "#text": "D-1000"
          }
        }
      },
      {
        "address_spec": {
          "@addr_no": "2",
          "full_address": "Harvard Univ, Dept Govt, Cambridge, MA 02138 USA",
          "organizations": {
            "@count": "2",
            "organization": [
              "Harvard Univ",
              {
                "@pref": "Y",
                "#text": "Harvard University"
              }
            ]
          },
          "suborganizations": {
            "@count": "1",
            "suborganization": "Dept Govt"
          },
          "city": "Cambridge",
          "state": "MA",
          "country": "USA",
          "zip": {
            "@location": "AP",
            "#text": "02138"
          }
        }
      }
    ]
  },
  "category_info": {
    "headings": {
      "@count": "1",
      "heading": "Social Sciences"
    },
    "subjects": {
      "@count": "3",
      "subject": [
        {
          "@ascatype": "traditional",
          "#text": "Economics"
        },
        {
          "@ascatype": "extended",
          "#text": "Business & Economics"
        },
        {
          "@ascatype": "traditional",
          "#text": "ECONOMICS"
        }
      ]
    }
  }
}
{
  "addresses": {
    "@count": "2",
    "address_name": [
      {
        "address_spec": {
          "@addr_no": "1",
          "full_address": "NYU, CV Starr Ctr Appl Econ, New York, NY 10003 USA",
          "organizations": {
            "@count": "2",
            "organization": [
              "NYU",
              {
                "@pref": "Y",
                "#text": "New York University"
              }
            ]
          },
          "suborganizations": {
            "@count": "1",
            "suborganization": "CV Starr Ctr Appl Econ"
          },
          "city": "New York",
          "state": "NY",
          "country": "USA",
          "zip": {
            "@location": "AP",
            "#text": "10003"
          }
        }
      },
      {
        "address_spec": {
          "@addr_no": "2",
          "full_address": "Princeton Univ, Princeton, NJ 08544 USA",
          "organizations": {
            "@count": "2",
            "organization": [
              "Princeton Univ",
              {
                "@pref": "Y",
                "#text": "Princeton University"
              }
            ]
          },
          "city": "Princeton",
          "state": "NJ",
          "country": "USA",
          "zip": {
            "@location": "AP",
            "#text": "08544"
          }
        }
      }
    ]
  },
  "category_info": {
    "headings": {
      "@count": "1",
      "heading": "Social Sciences"
    },
    "subjects": {
      "@count": "3",
      "subject": [
        {
          "@ascatype": "traditional",
          "#text": "Economics"
        },
        {
          "@ascatype": "extended",
          "#text": "Business & Economics"
        },
        {
          "@ascatype": "traditional",
          "#text": "ECONOMICS"
        }
      ]
    }
  }
}

What I was hoping to extract is a country for each of the records (some records have more than one country, which seems to be causing the problem). So my naive approach was to say:

我希望提取的是每个记录的国家(一些记录有多个国家,这似乎导致了这个问题)。所以我天真的做法是说:

.static_data."fullrecord_metadata".addresses.address_name.country

This however gives me several errors (null has no keys, and cannot index array with string). Checking using the keys command:

然而,这给了我几个错误(null没有键,也不能用字符串索引数组)。使用keys命令检查:

.static_data."fullrecord_metadata".addresses.address_name | keys

I can see that it's seems there's a problem with the way the data is structured...

我可以看到,数据结构的方式似乎存在问题......

So, could you suggest if I can actually extract the list of countries for each entry using jq? Thank you!

那么,您能否建议我是否可以使用jq实际提取每个条目的国家/地区列表?谢谢!

2 个解决方案

#1


3  

For each input top-level JSON entity, the following filter will recursively examine all the objects to see if they have a "country" key, and it will then report the distinct "country" values for that top-level entity:

对于每个输入顶级JSON实体,以下过滤器将递归检查所有对象以查看它们是否具有“country”键,然后它将报告该顶级实体的不同“country”值:

jq -c '[.. | if type == "object" and has("country") 
             then .country
             else empty end] | unique' 
["Israel"]
["USA"]
["Germany","USA"]
["USA"]
["Spain","USA"]
["USA"]
["USA"]
["USA"]
["Germany","USA"]
["USA"]

Here's a filter that will produce the same results in your example, though it is not exactly equivalent:

这是一个过滤器,可以在您的示例中生成相同的结果,但它不完全等效:

[.. | .country? // empty] | unique

[Exercise for the interested reader: what is the difference? :-) ]

[为感兴趣的读者练习:有什么区别? :-)]

#2


1  

Here is a solution which uses a function to handle the variation in .address_name

这是一个使用函数来处理.address_name中的变体的解决方案

 def address_specs:
    if type == "array" then .[].address_spec else .address_spec end
 ;

 .addresses | .address_name | [address_specs | .country] | unique

推荐阅读
  • 第四章高阶函数(参数传递、高阶函数、lambda表达式)(python进阶)的讲解和应用
    本文主要讲解了第四章高阶函数(参数传递、高阶函数、lambda表达式)的相关知识,包括函数参数传递机制和赋值机制、引用传递的概念和应用、默认参数的定义和使用等内容。同时介绍了高阶函数和lambda表达式的概念,并给出了一些实例代码进行演示。对于想要进一步提升python编程能力的读者来说,本文将是一个不错的学习资料。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • 本文介绍了使用哈夫曼树实现文件压缩和解压的方法。首先对数据结构课程设计中的代码进行了分析,包括使用时间调用、常量定义和统计文件中各个字符时相关的结构体。然后讨论了哈夫曼树的实现原理和算法。最后介绍了文件压缩和解压的具体步骤,包括字符统计、构建哈夫曼树、生成编码表、编码和解码过程。通过实例演示了文件压缩和解压的效果。本文的内容对于理解哈夫曼树的实现原理和应用具有一定的参考价值。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
  • 本文介绍了Codeforces Round #321 (Div. 2)比赛中的问题Kefa and Dishes,通过状压和spfa算法解决了这个问题。给定一个有向图,求在不超过m步的情况下,能获得的最大权值和。点不能重复走。文章详细介绍了问题的题意、解题思路和代码实现。 ... [详细]
author-avatar
东莞家装_670
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有