Encountering Keyerror while trying to parse JSON in Python

Recently, I developed a program for extracting data from an API that returns information in JSON format. However, when attempting to parse the data, I encountered a key error.

Traceback (most recent call last):
  File "test.py", line 20, in <module>
    print(parsed_json['plain'])
KeyError: 'plain'

The following snippet of code is crucial (the remaining portion deals with forming the URL and operates without any issues)

response = urllib.request.urlopen(url2).read()
strr = str(response)


if "plain" in strr:
    parsed_json = json.loads(response.decode("UTF-8"))
    print(parsed_json['plain'])
elif "INVALID HASH" in strr:
    print("You have entered an invalid hash.")
elif "NOT FOUND" in strr:
    print("The hash is not found")
elif "LIMIT REACHED" in strr:
    print("You have reached the max requests per minute, please try again in one minute.")

My objective is to extract data stored in the "plain" field. Below is a sample output retrieved from the API:

{
  "REQUEST": "FOUND",
  "739c5b1cd5681e668f689aa66bcc254c": {
    "plain": "test",
    "hexplain": "74657374",
    "algorithm": "MD5X5PLAIN"
  }
}

Answer №1

Understanding the structure of a JSON object is crucial when extracting specific data from it:

Demonstrated Example #1 — Tested with Python versions 2.6.9, 2.7.10, 3.3.5, and 3.5.0

import json

json_string = '''
{
    "REQUEST": "FOUND",
    "739c5b1cd5681e668f689aa66bcc254c": {
        "plain": "test",
        "hexplain": "74657374",
        "algorithm": "MD5X5PLAIN"
    }
}
'''

if 'plain' in json_string:
    parsed_json = json.loads(json_string)
    print(parsed_json['739c5b1cd5681e668f689aa66bcc254c']['plain'])

The key 'plain' pertains to the child element under '739c5b1cd5681e668f689aa66bcc254c'


Update

The next example iterates through parsed_json, validating each key for being 32 characters long and having a child value of 'plain' within:

Demonstrated Example #2 — Compatible with Python versions 2.6.9, 2.7.10, 3.3.5, and 3.5.0

import json
import re

def is_MD5(s):
    return True if re.match(r"([a-f\d]{32})", key) else False

strr = '''
{
    "REQUEST": "FOUND",
    "739c5b1cd5681e668f689aa66bcc254c": {
        "plain": "test",
        "hexplain": "74657374",
        "algorithm": "MD5X5PLAIN"
    }
}
'''

parsed_json = json.loads(strr)

for key, value in parsed_json.items():
    if is_MD5(key) and 'plain' in parsed_json[key]:
        xHash = key
        xPlain = parsed_json[key]['plain']

        print('the value of the key "plain" under the key "{0}" is "{1}"'.format(*[xHash,
                                                                    xPlain]))

Result

the value of the key "plain" under the key "739c5b1cd5681e668f689aa66bcc254c" is "test"

Answer №2

Within your dataset, the value of 'plain' is not found within the main structure of parsed_json. Instead, it resides inside

parsed_json['739c5b1cd5681e668f689aa66bcc254c']
. Therefore, accessing it would require using
parsed_json['739c5b1cd5681e668f689aa66bcc254c']['plain']
.

JSON operates on a hierarchical principle. The outer curly braces denote the entirety being one object assigned to parsed_json. Each part is represented as key-value pairs; here, the value corresponding to 'REQUEST' is 'FOUND'. On the other hand, the value under

'739c5b1cd5681e668f689aa66bcc254c'
represents a sub-object denoted by another set of brackets. This subsection includes keys such as 'plain', 'hexplain', and 'algorithm'. This concept is better explained through the following modification:

parsed_json: {
    "REQUEST":"FOUND",
    "739c5b1cd5681e668f689aa66bcc254c": {
        "plain":"test",
        "hexplain":"74657374",
        "algorithm":"MD5X5PLAIN"
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Python code to manage image files in a directory: "Maintain a designated number of images within a directory by using Python to

In my dataset, I have three main folders named Train, Test, and Val. Each of these folders contains over 100 subfolders. My goal is to only keep a maximum of 90 images in each folder. If a particular subfolder has more than 90 images, I need to delete th ...

Generate random numbers using a probability distribution skewed to the left

I am interested in selecting a number randomly between 1 and 100 in such a way that the likelihood of getting numbers from 60 to 100 is higher than those from 1 to 59. My goal is to create a left-skewed distribution for numbers 1-100, meaning it should ha ...

What is the best way to create a Python test case for a function with numerous print statements being executed?

I recently decided to dive into writing my own unit test cases. I successfully created tests for basic integer addition and subtraction functions, but now I wanted to tackle something more complex like a Password Checker. Here is the code snippet I've ...

Tips for showcasing API's JSON data in SwiftUI?

I have received JSON data from an API and I need to display a specific value from it as text. Here is a snippet of the JSON data: { "d": "2019-09-20", "v": 56.62 }, { "d": "2019-09-23", "v": 56.93 } To ha ...

Converting JSON to parquet format or not converting at all, the choice

We have encountered a situation where converting a JSON file to parquet results in the creation of numerous small parquet files. How can we prevent this from happening? What is the most effective and efficient approach to managing this transformation? Bel ...

What is the best way to analyze the similarities and differences between the nodes and edges within two

There are graphs of Berlin from January 1, 2020 and January 1, 2021. How can I compare the changes in edges and nodes between the two maps? import osmnx as ox import pandas as pd import networkx as nx import matplotlib.pyplot as plt from itertools import ...

What could be causing the alteration of my JSON data when sent through a WEB.API Ajax request?

Before Ajax Call: "{ "UnitOfMeasureRelatedUnitDataInbound": [ { "Name": "test", "Active": true, "UnitOfMeasureTypeID": "dd89f0a0-59c3-49a1-a2ae-7e763da32065", "BaseUnitID": "4c835ebb-60f2-435f-a5f4-8dc311fbbca0", "BaseUnitName": null, ...

Can you explain the significance of "Result" in the .map operator of an Observable fetched from an http.get call in NativeScript with Angular?

I'm currently working on the Nativescript/Angular tutorial and I stumbled upon a particular piece of code that has left me puzzled. In chapter 4 (Nativescript modules), there is a section where they make an http.get request to fetch the Grocery List a ...

Learning to interpret the attributes of a struct field

One of the key features in my package is the ability for users to define the backend database column name explicitly for their fields, if they choose to do so. Normally, the package uses the field's name as the column name. However, there are cases w ...

Steps to exclude the default data frame index when displaying a Python data frame in an Excel spreadsheet with the help of openpyxl

I am currently working on printing a pandas data frame in an Excel sheet using openpyxl. So far, I have successfully printed the data frame with the default index. However, my question now is how can I print it without the default index. The code snippet b ...

What is the best method for circumventing an express middleware?

I am currently working on an express application that utilizes several express routes, such as server.get('*' , ... ) to handle common operations like authentication, validation, and more. These routes also add extra information to the respon ...

Creating a JSON-Writable Class in Scala Play: A Step-by-Step Guide

Is there a way to create a function that converts case classes to Json, but only for classes with an implicit Writes defined? import play.api.libs.json._ def myJson(cc: Product): JsValue = { Json.toJson(cc) // simplified } Each case class needs an im ...

Python Selenium: Typing speed is too slow

When attempting to fill out a form quickly using element.send_keys("Anything"), I am finding that it takes significantly longer than expected. I have experimented with various Chromedriver versions, but the issue persists. Are there any possible reasons wh ...

Tips for speeding up the loading of JSON with large data on HTTP requests or webpages

When requesting the page (via HTTP or webpage), it seems to be very slow and even crashes unless I load my JSON data with fewer entries. This issue is critical as I anticipate needing to work with large amounts of data frequently in the future. Below are t ...

How to store a pointer to a pointer in Python?

Is it possible to store a reference to a reference in Python, allowing for the ability to change what that reference points to in another context? Consider the following scenario: class Foo: def __init__(self): self.standalone = 3 self.lst ...

Issue with graphviz software, encountering ExecutableNotFound error

Having trouble installing graphviz in PyCharm on my Windows system. I've already added the graphviz library to my PyCharm project, but I keep getting an error when trying to execute dot.render: graphviz.backend.execute.ExecutableNotFound: failed to e ...

Tips for efficiently zipping lists of varying sizes

I currently have 3 different lists. >>> list1 = [1,2,3,4,5] >>> list2 = ['a','b','c','d'] >>> list3 = ['x','y','z'] Is there a Python function that can zip t ...

Selenium encountering issues with loading additional content on Meetup tech page

I have been troubleshooting this issue for the past few days with no luck :( Is there anyone who can offer assistance? The problem I'm facing is that when selenium clicks "show more" on a specific city in the meetup website, it loads but nothing displ ...

Discover the methods for obtaining numerous JSON URLs and extracting dynamic information

I am currently in the process of developing my first application that extracts data from TMDB's API. Initially, I was successful in loading and parsing data from a single URL using the following code: uri = URI.parse("http://api.themoviedb.org/3/tv/ ...

Assistance requested in structuring JSON data

I'm currently working on making my javascript code more dynamic. Here's what I have so far: data.addRows(2); data.setValue(0, 0, 'name goes here'); data.setValue(0, 1, value 1 goes here); data.setValue(0, 2, value 2 goes here); data. ...