首页 > 解决方案 > 如何将要由脚本解析的 JSON 的哪个键存储在另一个 JSON 中?

问题描述

假设我有一个名为 jsondata.json 的 JSON:

{
  "apiURL": [
    {
      "name":"Target", 
      "url":"https://redsky.target.com/v2/plp/collection/13562231,14919690,13728423,14919033,13533833,13459265,14917313,13519319,13533837,14919691,13479115,47778362,15028201,51467685,50846848,50759802,50879657,13219631,13561421,52062271,14917361,51803965,13474244,13519318?key=eb2551e4accc14f38cc42d32fbc2b2ea&pricing_store_id=2088&multichannel_option=basics&storeId=321"
    },
    {
      "name":"Safeway",
      "url":"https://shop.safeway.com/bin/safeway/product/aemaisle?aisleId=1_23_2&storeId=1483"
    }
  ]
}

我想告诉我的脚本从 url 包含的 API 中检索数据,如下所示:

# Load JSON containing URLs of APIs of grocery stores
with open(json_data, 'r') as data_f:
    data_dict = json.load(data_f)

# Organize API URLs
for apiurl in data_dict['apiURL']:
    responses.append('')
    responses[index] = requests.get(apiurl['url'])
    responses[index].raise_for_status()
    storenames.append(apiurl['name'])
    index += 1
first_target_item = responses[0].json()['search_response']['items']['Item'][0]['title']
first_safeway_item = responses[1].json()['productsinfo'][0]['description']

正如您所看到的,我当前的实现需要我手动输入脚本来从每个 API 解析哪个键(最后两行)。我希望最终能够从动态数量的杂货店中检索信息,但是每个网站都将其商品的数据存储在其 API 的不同密钥中。

如何使过程自动化(例如,将要解析的密钥存储在 jsondata.json 中),这样每次添加新杂货店时就不必更新脚本?

标签: pythonjsonpython-3.x

解决方案


如果您可以修改 jsondata.json ,您可以保留这样的数组:

{
  "name":"Target", 
  "accessKeys": ["search_response", "items", "Item", "0", "title"],
  "url":"https://redsky.target.com/v2/plp/collection/13562231,14919690,13728423,14919033,13533833,13459265,14917313,13519319,13533837,14919691,13479115,47778362,15028201,51467685,50846848,50759802,50879657,13219631,13561421,52062271,14917361,51803965,13474244,13519318?key=eb2551e4accc14f38cc42d32fbc2b2ea&pricing_store_id=2088&multichannel_option=basics&storeId=321",
}

在您的 python 代码中:

keys=["search_response", "items", "Item", "0", "title"] #apiUrl['accessKeys']
target_item=responses[0].json()
for i in target_item:
    target_item=target_item[i]

你可以自动化更多,

def get_keys(data, keys):
    for key in keys:
        data=data[key]
    return data
items=[]
for index, apiurl in enumerate(data_dict['apiURL']):
    responses.append('')
    responses[index] = requests.get(apiurl['url'])
    responses[index].raise_for_status()
    storenames.append(apiurl['name'])
    items.append(get_keys(responses[index].json(), apiUrl['accessKeys']))

推荐阅读