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. The loads 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]}]'