1

I am trying to extract certain positions from AWS pricelist, but I am keeping getting the below error code. Is there an error with JSON structure ?

Traceback (most recent call last):
  File "/Python/python2.py", line 5, in <module>
    for r in data['SJQU8C5FNVFYMK7X']:
KeyError: 'SJQU8C5FNVFYMK7X

JSON FILE

"SJQU8C5FNVFYMK7X" : {
  "sku" : "SJQU8C5FNVFYMK7X",
  "productFamily" : "Compute Instance",
  "attributes" : {
    "servicecode" : "AmazonEC2",
    "location" : "Asia Pacific (Singapore)",
    "locationType" : "AWS Region",
    "instanceType" : "r5n.8xlarge",
    "currentGeneration" : "Yes",
    "instanceFamily" : "Memory optimized",
    "vcpu" : "32",
    "physicalProcessor" : "Intel Xeon Platinum 8259 (Cascade Lake)",
    "clockSpeed" : "2.5 GHz",
    "memory" : "256 GiB",
    "storage" : "EBS only",
    "networkPerformance" : "25 Gigabit",
    "processorArchitecture" : "64-bit",
    "tenancy" : "Shared",
    "operatingSystem" : "Windows",
    "licenseModel" : "No License required",
    "usagetype" : "APS1-BoxUsage:r5n.8xlarge",
    "operation" : "RunInstances:0102",
    "capacitystatus" : "Used",
    "dedicatedEbsThroughput" : "5000 Mbps",
    "ecu" : "NA",
    "enhancedNetworkingSupported" : "No",
    "intelAvxAvailable" : "No",
    "intelAvx2Available" : "No",
    "intelTurboAvailable" : "No",
    "normalizationSizeFactor" : "64",
    "preInstalledSw" : "SQL Ent",
    "servicename" : "Amazon Elastic Compute Cloud"
  }

Python code:

import json

with open('index (5).json') as json_file:
    data = json.load(json_file)
    for r in data['SJQU8C5FNVFYMK7X']:
        print (r)

Any ideas what I am doing wrong ?

3
  • can you show me your data variable ? Commented Mar 25, 2020 at 13:59
  • Considering the JSON you shared is you data variable, I don't see a problem. I just run your code it worked fine. It printed: sku, productFamily and attributes. Was that what you wanted to print? If you wanted to print the attributes, you should have used data['SJQU8C5FNVFYMK7X']['attributes']. Commented Mar 25, 2020 at 14:17
  • The only thing I need to point out is that you would need some extra curly brackets in your json for this to properly work, otherwise you will get syntax errors. Commented Mar 25, 2020 at 14:18

2 Answers 2

2

You are missing some curly brackets around the whole thing, the json below parses properly

{
    "SJQU8C5FNVFYMK7X": {
        "sku": "SJQU8C5FNVFYMK7X",
        "productFamily": "Compute Instance",
        "attributes": {
            "servicecode": "AmazonEC2",
            "location": "Asia Pacific (Singapore)",
            "locationType": "AWS Region",
            "instanceType": "r5n.8xlarge",
            "currentGeneration": "Yes",
            "instanceFamily": "Memory optimized",
            "vcpu": "32",
            "physicalProcessor": "Intel Xeon Platinum 8259 (Cascade Lake)",
            "clockSpeed": "2.5 GHz",
            "memory": "256 GiB",
            "storage": "EBS only",
            "networkPerformance": "25 Gigabit",
            "processorArchitecture": "64-bit",
            "tenancy": "Shared",
            "operatingSystem": "Windows",
            "licenseModel": "No License required",
            "usagetype": "APS1-BoxUsage:r5n.8xlarge",
            "operation": "RunInstances:0102",
            "capacitystatus": "Used",
            "dedicatedEbsThroughput": "5000 Mbps",
            "ecu": "NA",
            "enhancedNetworkingSupported": "No",
            "intelAvxAvailable": "No",
            "intelAvx2Available": "No",
            "intelTurboAvailable": "No",
            "normalizationSizeFactor": "64",
            "preInstalledSw": "SQL Ent",
            "servicename": "Amazon Elastic Compute Cloud"
        }
    }
}

Note that you can use online linter to check for parsing errors (e.g https://jsonlint.com/)

Sign up to request clarification or add additional context in comments.

Comments

1

The easy way to debug this is to open an interactive interpreter session and look at what keys are present:

>>> import json
>>> json_file = open('index (5).json')
>>> data = json.load(json_file)
>>> data.keys()

you might want to cast data.keys() as a list (ie. list(data.keys())) if you want to manipulate it at all. But at least this will show you what json.load thinks the keys are.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.