Python JSON parsing example



#Created on Jul 30, 2013
#@author: tunatore

import json

# you can create a json structure
pythonStructure = { 'JavaLevel':'Expert', 'PythonLevel':"Beginner", 'C#Level':"Good" }

print ("Python Structure = " , pythonStructure)

#dumps will load python structure as json
print ("JSON Converted Structure = ", json.dumps(pythonStructure))
print ("JSON Converted Structure Sorted Keys = " , json.dumps(pythonStructure, sort_keys=True))

#pretty printed JSON
print ("pretty printed JSON \n")
print (json.dumps(pythonStructure, sort_keys=True, indent=5, separators=(',', ' : ')))

#dumps will load json structure as python structure
print ("Python Structure = " , (json.loads(json.dumps(pythonStructure))))

#another python struct conversion

pythonData = dict(
 key1 = "Val 1",
 key2 = "Val 2",
 key3 = "Val 3",
 key4 = 4
)

print ("JSON Converted Structure = ", json.dumps(pythonData,sort_keys=True))
#loading and parsing JSON data from url
import urllib.request
url = "http://carma.org/api/1.1/searchLocations?name=Idaho"
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
loadJSON = json.loads(response.read().decode('utf-8'))
print (json.dumps(loadJSON, indent=4, sort_keys=True))

# get carbon JSON item
carbon = loadJSON[0]['carbon']
print ("carbon future" , carbon['future'])
print ("carbon past" , carbon['past'])
print ("carbon present" , carbon['present'])

Output;

Python Structure = {‘PythonLevel’: ‘Beginner’, ‘C#Level’: ‘Good’, ‘JavaLevel’: ‘Expert’}
JSON Converted Structure = {“PythonLevel”: “Beginner”, “C#Level”: “Good”, “JavaLevel”: “Expert”}
JSON Converted Structure Sorted Keys = {“C#Level”: “Good”, “JavaLevel”: “Expert”, “PythonLevel”: “Beginner”}
pretty printed JSON

{
“C#Level” : “Good”,
“JavaLevel” : “Expert”,
“PythonLevel” : “Beginner”
}
Python Structure = {‘PythonLevel’: ‘Beginner’, ‘C#Level’: ‘Good’, ‘JavaLevel’: ‘Expert’}
JSON Converted Structure = {“key1”: “Val 1”, “key2”: “Val 2”, “key3”: “Val 3”, “key4”: 4}
[
{
“carbon”: {
“future”: “2068400.0000”,
“past”: “758970.0000”,
“present”: “838420.0000”
},
“country”: {
“id”: “6252001”,
“name”: “United States”
},
“energy”: {
“future”: “18787000.0000”,
“past”: “10677000.0000”,
“present”: “13059000.0000”
},
“fossil”: {
“future”: “0.2371”,
“past”: “0.1666”,
“present”: “0.1339”
},
“hydro”: {
“future”: “0.5648”,
“past”: “0.7809”,
“present”: “0.7996”
},
“id”: “5596512”,
“intensity”: {
“future”: “110.0000”,
“past”: “71.1000”,
“present”: “64.2000”
},
“name”: “Idaho”,
“nuclear”: {
“future”: “0.0000”,
“past”: “0.0000”,
“present”: “0.0000”
},
“plant_count”: “92”,
“renewable”: {
“future”: “0.1981”,
“past”: “0.0525”,
“present”: “0.0664”
},
“type”: “province”
}
]
carbon future 2068400.0000
carbon past 758970.0000
carbon present 838420.0000

Advertisements

6 thoughts on “Python JSON parsing example

  1. Okay first of all i think you should write the output directly after writing the python code. It should not be written separately because many beginners will be confused by this 🙂 (I am not a beginner, just a helpful response)

      1. It seems nice, you might change the design of your blog for easy of reading.
        You can also add some widgets, it will be good to see archives or other widgets for navigating etc.

      2. It’s all available on the top. The archives are there you just have to hit a button and a menu will open. However i will look into changing the theme. But not just now.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s