gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:30:56 26 4
gpt4 key购买 nike

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

这是一个示例代码:

{
"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"
}
]
}
}
}

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

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

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

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

我可以看出数据的结构方式似乎有问题...

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

最佳答案

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

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"]

这是一个过滤器,它会在您的示例中产生相同的结果,尽管它并不完全相同:

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

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

关于json - 使用 jq 提取可变数量的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34367613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com