Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Placeholder text not disappearing in Contact Form 7 2 23
JS does not refresh 6 30
Dynamic Dropdowns 15 32
Using toggleClass to resize Bootstrap columns 7 16
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…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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…

808 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