从 json 文件中提取字段

时间:2021-06-17 14:03:05

标签: python json

我得到了以下 json 文件。

{
  "results": [
    {
      "id": "1234",
      "useless_field1": null,
      "useless_field2": null,
      "data": [
        {
          "type": "type_1",
          "useless_field3": null,
          "volumne": "5"
        }
      ]
    }
  ]
}

我只想提取以下字段:id, type, volume。我是通过以下方式做到的。

def extract(json_response):
    return [{
        item['id'],
        item['data'][0]['type'],
        item['data'][0]['volume']
    } for item in json_response['results']]

type 可能是一个列表。我的解决方案有效吗?是否有解决此类问题的替代方案?

0 个答案:

没有答案
相关问题