0

I am creating a python script to extract values, Name from the JSON Key Details from the JSON result. Python error mentioned KeyError 'details[name]'. The JSON example is found below. The JSON is incomplete. JSON has other data which I am not going to put it here as it is confidential.

details: {'id': 5555, 'name': 'Timothy', 'Gender': 'Male'}

My Python script is shown below

print(json_data['details[name]'])

Error Message

print(json_data['details[name]'])
KeyError: 'details[name]'

I want to print the result

Timothy 

What am I missing?

1
  • The syntax you want is json_data['details']['name'] Commented Jul 27, 2022 at 2:04

2 Answers 2

1

Assuming json_data is the name you've chosen for the entirety of the JSON object, you need provide a proper index for json_data in your print statement. Your current index is a string because you've captured your brackets inside of the apostrophes. The way you're indexing the JSON object is also incorrect.

JSON objects are essentially multidimensional dictionaries. The way you print the value of a value of a key is like this: print(dict["key"]["value"]).

Assuming the details key is also a string, the correct way to print the value of name from the key "details" is: print(json_data["details"]["name"]

Example

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

Comments

0

do it one key at a time rather than trying to fit both the keys into one set of quotes as you originally had.

print(json_data['details']['name'])

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.