Python json - ghdrako/doc_snipets GitHub Wiki
JSON data is structured as JSON objects in the form of key-value pairs scoped by a pair of curly braces, such as {"title": "Laundry", "desc": "Wash clothes", "urgency": 3}
. JSON objects require their keys to be only strings, and this require-ment allows the standard communication between different systems. The values include strings and integers, but JSON supports other data types, including Boolean, arrays (like list in Python), and objects.
JSON strings must be enclosed only in double quotes.
Python strings can use single or double quotes
JSON supports nested data structures. A JSON object can hold another JSON object, for example. An array can be a list of any supported data types, including objects. Following are some examples:
embedded object: {"one": {"one": 1}, "two": {"two": 2}}
array of strings: ["one", "two", "three"]
JSON | Python |
---|---|
String: "one" | str "one" |
Number, integer: 123 | int: 123 |
Number, real: 2.5 | float: 2.5 |
Boolean: true | bool: True |
Array: [1, 2] | list: [1, 2] |
Object: {''one'': 1} | dict: {''one'': 1} |
Null: null | NoneType: None |
Python doesn’t have a native data type that matches numbers in JSON objects, which don’t differenti-ate integers from floating-point numbers and refer to them as numbers collectively.
Python uses int and float to represent JSON numbers when they’re inte-gers or real numbers.
Deserializing JSON strings
tasks_json = """
[ { "title": "Laundry", "desc": "Wash clothes", "urgency": 3 }, { "title": "Homework", "desc": "Physics + Math", "urgency": 5 } ]
"""
import json
tasks_read = json.loads(tasks_json)
print(tasks_read)
# output: [{'title': 'Laundry', 'desc': 'Wash clothes', 'urgency': 3}, ➥ {'title': 'Homework', 'desc': 'Physics + Math', 'urgency': 5}] str: ''one'' int: 123 float: 2.5 bool: True list: [1, 2] dict: {''one'': 1}
Converting dict objects to instances of a custom class
from dataclasses
import dataclass
@dataclass
class Task:
title: str
desc: str
urgency: int
@classmethod
def task_from_dict(cls, task_dict):
return cls(**task_dict)
tasks = [Task.task_from_dict(x) for x in tasks_read] print(tasks)
# output: [Task(title='Laundry', desc='Wash clothes', urgency=3), ➥ Task(title='Homework', desc='Physics + Math', urgency=5)]
We successfully converted the list of dict objects to a list of Task instance objects, as we planned.
- We use the
dataclass
decorator on the Task class so that we don’t have to implement the boilerplate for__init__
and__repr__
. - The cls argument in the class method task_from_dict refers to the class Task.
- We know that
**kwargs
refers to the variable number of keyword arguments and is packed as a dict object. Conversely, to access the key-value pairs, the**
operator converts the dict object to keyword arguments, which the constructor uses to create a new instance of the Task class. We’ve seen how to convert a JSON array to a list object in Python. Theloads
method is flexible. The method does more than convert JSON arrays; it can also parse any JSON data types other than objects. Following are a few examples:
json.loads("2.2")
# output: 2.2
json.loads('"A string"')
# output: 'A string'
Serializing Python data to JSON format
builtin_data = ['text', False, {"0": None, 1: [1.0, 2.0]}]
builtin_json = repr(json.dumps(builtin_data))
print(builtin_json)
# output: '["text", false, {"0": null, "1": [1.0, 2.0]}]'
Reading JSON Files
import json
with open(’data.json’, encoding=’utf-8’) as json_file:
data = json.load(json_file) print(data)
This operation translates JSON arrays and objects into corresponding Python lists and dictionaries, facilitating direct data manipulation or analysis.
Writing JSON Files
import json
data = {’key’: ’value’, ’numbers’: [1, 2, 3]}
with open(’output.json’, ’w’, encoding=’utf-8’) as json_file:
json.dump(data, json_file, indent=4)
The indent parameter is available for arranging JSON content, aiding human-readability, crucial during development or debugging phases.
Advanced JSON Handling
Custom Serialization
Sometimes, data structures exceed basic JSON capabilities; Python permits customization through overriding JSONEncoder for bespoke types.
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, CustomType):
return {’value’: obj.value}
return super().default(obj)
This strategy allows expanded functionality while maintaining structured data integrity during external storage operations.
Decoding Complex JSON
def custom_decoder(dct):
if 'date' in dct:
dct['date'] = parse_date(dct['date'])
return dct
with open('complex_data.json', 'r',encoding=’utf-8’) as json_file:
data = json.load(json_file, object_hook=custom_decoder)
Convert CSV to JSON
import csv
import json
def convert_csv_to_json(csv_filename,json_filename):
with open(csv_filename, newline='',encoding='utf-8') as csv_file:
csv_reader = csv.DictReader(csv_file)
data_list = list(csv_reader)
with open(json_filename, 'w',encoding='utf-8') as json_file:
json.dump(data_list, json_file,indent=4)
convert_csv_to_json('input.csv', 'output.json')