Skip to main content

Python JSON Parsing


In this tutorial you will learn:

  • JSON
  • JSON parsing in Python
  • Converting to JSON

JSON

JSON simply means Javascript Object Notation. It’s a standard format for exchanging data. It provides a simple way to organize information in easily readable format. JSON is important in Python because Python can also handle backend and front end web development tasks. An excellent library used for web development tasks in Python is called django.

myJson = '{"Test": 1, "First Name": "Python", "Last Name": "Programming", "numbers": [1334, 445]}'

JSON parsing in Python

To do JSON parsing in Python we first need to import json module which has a lot of helper functions that can help us in parsing the JSON. This most important function is the loads function which converts the JSON in the form of a Dictionary object which can be easily handled in Python.

Example:

  1. import json
  2. print("** JSON Parsing **")
  3. myJson = '{"Test": 1, "First Name": "Python", "Last Name": "Programming", "numbers": [1334, 445]}'
  4. parsed = json.loads(myJson)
  5. print(parsed["First Name"])
  6. print(parsed["Last Name"])
  7. print(parsed["numbers"][1])

In this example the loads functions gives us the result in Dictionary form which is saved in parsed variable and printed using print commands.

  1. employee = '{"empName": "Mike", "age": 60, "skills": ["Python", "C++", "C#"], "kids": {"john": { "age": 30, "occupation": "Banker"}, "Tom": {"age": 25, "occupation": "Engineer"}}}'
  2. emp = json.loads(employee)
  3. print( emp["empName"])
  4. print( emp["age"])
  5. print(emp["kids"]["Tom"]["occupation"])

In this example we parsed a nested json using the loads function and we can note that for three levels of nesting we used 3 sets of square brackets to reach the required value.

Converting to JSON

Just like loads function allows us to convert a JSON to a Dictionary similarly there is another function in json module called dumps which allows us to convert Dictionary object to JSON.

Example:

  1. emp = {
  2. "empAge": 55,
  3. "salary": 494,
  4. "deparment": "IT",
  5. "empName": "Mike",
  6. }
  7. js = json.dumps(emp)
  8. print("Json is:", js)

Here we can see that how easy it is to convert Python Dictionary object to json by simply passing it to the json.dumps function.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.