purple_rein
purple_rein

Reputation: 5

Json extraction of specfic field via Python

Trying to get the "externalCode" field from the below incomplete json file, however i am lost, i used python to only get to second element and get the error. I am not sure how to go about traversing through a nested JSON as such below

 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 file

[
 {
"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%"
          }
        ]
      },
      {

Upvotes: 0

Views: 94

Answers (2)

Jed Fox
Jed Fox

Reputation: 3025

Try this code:

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']

Upvotes: 1

kevinpowe
kevinpowe

Reputation: 496

I'm not quite sure I understand what you're looking to do with your code. There are multiple externalCode values for each element in the array, at least from the sample you've posted. But you can get the data you're looking for with this syntax:

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

The code below iterates through the data you're interested in (with a slightly modified JSON file so that it's complete) and works as desired:

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"

Upvotes: 0

Related Questions