Python JSON Module

JSON stands for JavaScript Object Notation and is a lightweight format for storing and transporting data. JSON is often used when data is sent from a server to a web page.

Python has the built-in module json, which allow us to work with JSON data.

import json

JSON Data Types

A JSON object is similar to a Python dictionary, but has the following differences:

  • JSON Keys are always string.
  • Strings are always enclosed with double quotes.
  • A JSON boolean start with lowercase letters.
  • null is the JSON equivalent of Python None.

The data types JSON supports are:

  • String
  • Number
  • boolean
  • null
  • Object
  • Array

Example:

{
  "name": "Charles",
  "age": 33,
  "has_hair": false,
  "hobbies": ["photography", "running"],
  "appearance": {
    "eyes": "brown",
    "hair_color": null
  }
}

JSON loads() method

With the json.loads method, you can parse a JSON object and transform it to a Python dictionary:

import json

# JSON string to parse
json_person = '{"name": "Charles", "age": 33, "has_hair": false, "hobbies": ["photography", "running"]}'
# Parse JSON string into Python dictionary
python_person = json.loads(json_person)
python_person
{'name': 'Charles', 'age': 33, 'has_hair': False, 'hobbies': ['photography', 'running']}
type(python_person)
<class 'dict'>
# Access dictionary value using get method
python_person.get("name")
'Charles'

JSON dumps() method

The other way around. The dumps() method transforms a Python object to a JSON string:

import json

# Python dictionary to convert
python_person = {'name': 'Charles', 'age': 33, 'has_hair': False, 'hobbies': ['photography', 'running']}
# Convert Python object to JSON string
json_person = json.dumps(python_person)

json_person
'{"name": "Charles", "age": 33, "has_hair": false, "hobbies": ["photography", "running"]}'
type(json_person)
<class 'str'>

Reading and writing JSON Files

Reading a JSON File

import json
# Read JSON file
with open("filename.json", "r") as f:
    json_content = json.loads(f.read())

# Note: This line seems redundant - json_content is already parsed
json.loads(json_content)
{'name': 'Charles', 'age': 33, 'has_hair': False}

Writing a JSON File

import json

person = {'name': 'Charles', 'age': 33}

# Write Python dictionary to JSON file
with open("filename.json", "w") as f:
    f.write(json.dumps(person))
Morty Proxy This is a proxified and sanitized view of the page, visit original site.