用Python实现特定字段的Json提取

2024-10-01 04:45:13 发布

您现在位置:Python中文网/ 问答频道 /正文

试图从下面不完整的json文件中获取“externalCode”字段,但是我迷路了,我使用python只获取第二个元素并获取错误。我不知道如何遍历下面这样的嵌套JSON

 output.writerow([row['benefitCategories'], row['benefitValueSets']] + row['disabled'].values())

KeyError:'benefitValueSets'

import csv, json, sys

input = open('C:/Users/kk/Downloads/foo.js', 'r')
data = json.load(input)
input.close()

 output = csv.writer(sys.stdout)

 output.writerow(data[0].keys())  # header row

 for row in data:
     output.writerow([row['benefitCategories'], row['benefitValueSets']] +          row['disabled'].values())

Json文件

[
 {
"benefitCategories": [
  {
    "benefits": [
      {
        "benefitCode": "NutritionLabel",
        "benefitCustomAttributeSets": [

        ],
        "benefitValueSets": [
          {
            "benefitValues": [
              null
            ],
            "costDifferential": 0,
            "default": false,
            "disabled": false,
            "displayValue": "$500",
            "externalCode": null,
            "id": null,
            "internalCode": "$500",
            "selected": false,
            "sortOrder": 0
          }
        ],
        "configurable": false,
        "displayName": "DEDUCTIBLE",
        "displayType": null,
        "externalCode": "IndividualInNetdeductibleAmount",
        "id": null,
        "key": "IndividualInNetdeductibleAmount",
        "productBenefitRangeValue": null,
        "sortOrder": 0,
        "values": [
          {
            "code": null,
            "description": null,
            "id": null,
            "numericValue": null,
            "selected": false,
            "value": "$500"
          }
        ]
      },
      {
        "benefitCode": "NutritionLabel",
        "benefitCustomAttributeSets": [

        ],
        "benefitValueSets": [
          {
            "benefitValues": [
              null
            ],
            "costDifferential": 0,
            "default": false,
            "disabled": false,
            "displayValue": "100%",
            "externalCode": null,
            "id": null,
            "internalCode": "100%",
            "selected": false,
            "sortOrder": 0
          }
        ],
        "configurable": false,
        "displayName": "COINSURANCE",
        "displayType": null,
        "externalCode": "PhysicianOfficeInNetCoInsurancePct",
        "id": null,
        "key": "PhysicianOfficeInNetCoInsurancePct",
        "productBenefitRangeValue": null,
        "sortOrder": 0,
        "values": [
          {
            "code": null,
            "description": null,
            "id": null,
            "numericValue": null,
            "selected": false,
            "value": "100%"
          }
        ]
      },
      {

Tags: idjsonfalseinputoutputnullrowvalues
2条回答

我不太确定我是否理解你想用你的代码做什么。数组中的每个元素都有多个externalCode值,至少来自您发布的示例。但您可以使用以下语法获取所需的数据:

data[0]["benefitCategories"][0]["benefits"][0]["externalCode"]
data[0]["benefitCategories"][0]["benefits"][1]["externalCode"]

下面的代码遍历您感兴趣的数据(使用稍加修改的JSON文件使其完整),并按需要工作:

import csv, json, sys

input = open('junk.json', 'r')
data = json.load(input)
input.close()

for x in data[0]["benefitCategories"][0]["benefits"]:
    print x["externalCode"] + "\n\n"

请尝试以下代码:

import csv, json, sys

input = open('C:/Users/spolireddy/Downloads/foo.js', 'r')
data = json.load(input)
input.close()

 output = csv.writer(sys.stdout)

 output.writerow(data[0].keys())  # header row

 for row in data:
     output.writerow([row['benefitCategories'], row['benefitCategories'][0]['benefits'][0]['benefitValueSets'][0], row['benefitCategories'][0]['benefits'][0]['benefitValueSets'][0]['disabled']])
      # for externalCode:
      row['benefitCategories'][0]['benefits'][0]['benefitValueSets'][0]['externalCode']

相关问题 更多 >