Solved

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

Posted on 2016-10-05
3
66 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 16

Accepted Solution

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

Author Comment

by:alexmac05
ID: 41830940
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
ID: 41830941
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Angular - code works but need to make a change 14 30
how can i apply an assert arrayEqual function ? 2 26
HTML5 frame 5 25
Checking data with javascript 3 19
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

770 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