将 json 存储为可搜索的 python 对象?

store json as a searchable python object?

我有一个巨大的混乱网站,我已将其作为 json 加载到我的代码中,以便使用 scrapy 进行网页抓取:json link

我想存储这个 json,这样我就可以访问存储在 group_attribute 子集中的 attribute_labels 和 attribute_values。但这是严重嵌套的,因此遍历 json 的计算量太大了。我的最终目标是能够使用一些 SQL 类型的逻辑,例如“where attribute_label='series' get attribute_value”。我的代码中的其他地方也有类似的逻辑,但同样,由于嵌套的程度,它在这里不起作用。非常感谢任何建议或想法,谢谢!

当我在 python 中使用 JSON 时,我只使用 json

import requests,json

url = 'your_url'
response = requests.get(url)
data = response.json()

#print(json.dumps(data,indent=2)) # This will prettyprint the json

items = data['data']['productDetail']['items']

for item in items:
    print(item['id'])

我不认为这对计算机来说很难迭代。如果您找到系统遵循的模式,那么您只需转到 json 中您需要的位置,然后执行搜索。

您可以结帐JMESPath, a query language for JSON. It has a Python library.

这是自述文件中的示例:

>>> import jmespath
>>> expression = jmespath.compile('foo.bar')
>>> expression.search({'foo': {'bar': 'baz'}})
'baz'
>>> expression.search({'foo': {'bar': 'other'}})
'other'