如何使用JSONPATH从嵌套对象的JSON数组中获取多个值?

jv4diomz  于 2022-12-24  发布在  其他
关注(0)|答案(3)|浏览(601)

我需要使用JSONPATH从包含嵌套对象的JSON数组(下面的JSON示例)中获取多个值。

{
    "store": {
       "name": "ABC Co",
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95,
                "edition": {
                       "year": 1990,
                       "published_by": "MacMillan"
                 }
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99,
                "edition": {
                       "year": 1980,
                       "published_by": "MacMillan"
                 }
            }
        ]
     }
}

当我尝试的时候,这在父对象级别上工作得很好。
x一个一个一个一个x一个一个二个x
现在我也想获取子对象值(edition.year),如下所示:

["Sayings of the Century", "Nigel Rees", 1990]

但下面的表达式不起作用:

store.book[0].[title,author,edition.year]

有人能帮忙吗?谢谢。

q9rjltbz

q9rjltbz1#

对于JSONPath-Plus,您可以使用以下查询

store.book[0].[title,author,year]

测试地点:https://jsonpath.com/

edqdpe6u

edqdpe6u2#

这里使用的特性称为union。虽然没有绑定标准来说明如何在JsonPath库中实现union,但大多数只允许两个属性的两个union。C. Burgmer编译了不同JsonPath implementations and their behavior的概述,甚至没有列出用于联合两个以上(平面)属性的特性。因此,这是特定于实现的,大多数不支持它。
我发现的下一个最好的方法是使用Goessner的JsonPath或JsonPath-Plus(都是JavaScript)在同一级别上联合三个属性:

$.store.book[0].[title,author,edition]

结果是

[
  "Sayings of the Century",
  "Nigel Rees",
  {
    "year": 1990,
    "published_by": "MacMillan"
  }
]

您可以在线试用herehere (Goessner tab)

bvuwiixz

bvuwiixz3#

d = {
        "store": {
           "name": "ABC Co",
            "book": [
                {
                    "category": "reference",
                    "author": "Nigel Rees",
                    "title": "Sayings of the Century",
                    "price": 8.95,
                    "edition": {
                           "year": 1990,
                           "published_by": "MacMillan"
                     }
                },
                {
                    "category": "fiction",
                    "author": "Evelyn Waugh",
                    "title": "Sword of Honour",
                    "price": 12.99,
                    "edition": {
                           "year": 1980,
                           "published_by": "MacMillan"
                     }
                }
            ]
         }
    }
    
    def getbook(list_,d):
        if isinstance(d,dict):
            for k,v in d.items():
                if k == 'title':list_.append(v)
                
                elif k == 'author' : list_.append(v)    
                    
                elif isinstance(v,list): 
                    for x in v :getbook(list_, x)
                else : getbook(list_, v)        
                        
        return list_
    
    aa = getbook([], d)
    print(aa)
    
    """
    ['Nigel Rees', 'Sayings of the Century', 'Evelyn Waugh', 'Sword of Honour']
    """
Method 2 : 

list_=[]
def recurse(d):
        if isinstance(d, dict):
            for k, v in d.items():
                if k == 'author':list_.append(v)
                elif k == 'title':list_.append(v)
                if isinstance(v, (dict, list)):
                    recurse(v)

        elif isinstance(d,list):
            for item in d:
                if item == 'author':list_.append(item)
                elif item == 'title':list_.append(item)
                if isinstance(item, (dict, list)):
                    recurse(item)
        else: return
recurse(d1)
print(list_)

print("...........")

相关问题