Solved

python - JSON needs to be converted to a python dictionary (has some nested json)

Posted on 2016-10-05
3
36 Views
Last Modified: 2016-10-05
I need a way to convert a string like this:

{
    "event": {
                      "event_type":"method_Name",
                      "event_time":"1475692166",
                      "event_hash":"1234",
                      "event_metadata": {
                                                          "related_firstName":null,
                                                          "reported_secondName":"1234",
                                                          "reported_for_ID":null,
                                                          "event_message":null
                                                    }
                }
 }

into a python dictionary and I need to deal with the nested JSON array's as well.
0
Comment
Question by:alexmac05
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
Walter Ritzel earned 500 total points
Comment Utility
[code]
import json
new_dict = json.loads("{'1':'text'}")
print(new_dict)
[\code]
0
 

Author Comment

by:alexmac05
Comment Utility
strData = '''
{"event": {"event_type":"method_Name","event_time":"1475692166","event_hash":"1234","event_metadata": {"related_firstName":null,"reported_secondName":"1234","reported_for_ID":null,
"event_message":null}}}'''


#print(strData)

new_dict = json.loads(strData)
print(type(new_dict))
print(new_dict['event']['event_metadata']['reported_secondName'])
0
 

Author Closing Comment

by:alexmac05
Comment Utility
This is the code with Walter's answer


strData = '''
{"event": {"event_type":"method_Name","event_time":"1475692166","event_hash":"1234","event_metadata": {"related_firstName":null,"reported_secondName":"1234","reported_for_ID":null,
"event_message":null}}}'''


#print(strData)

new_dict = json.loads(strData)
print(type(new_dict))
print(new_dict['event']['event_metadata']['reported_secondName'])
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now