python’s commonly used built-in module json

introduce

When dealing with JSON data, the json module in Python provides four main functions: dump, dumps, load and loads. These functions provide functionality to convert and serialize between JSON data and Python objects.

JSON (JavaScript Object Notation) is a lightweight data exchange format widely used to transfer data from one application to another. It is based on a subset of the JavaScript language but has become a common data format across programming languages ​​and platforms.

JSON data consists of key-value pairs, similar to dictionaries in Python or objects in JavaScript. It supports the following data types:

  • String: Represents text data, enclosed in double quotes.
  • Number: represents an integer or floating point number.
  • Boolean: represents true or false.
  • Array: Represents an ordered list of values, enclosed in square brackets and separated by commas.
  • Object: Represents a collection of key-value pairs, enclosed in curly braces, separated by colons between keys and values, and separated by commas between key-value pairs.

Commonly used APIs

  • **loads() **: Parses a JSON string into a Python object.

    Notice:

    • When using loads, the string in json must use double quotes, otherwise an error will be reported (json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 3 column 5 (char 12))
    • The input parameter cannot be None, otherwise an error will be reported
  • dumps() : Convert Python object to JSON string

    parameter:

    • obj (required): A Python data structure to encode into JSON format. This can be a dictionary, list, integer, string, etc.

    • ensure_ascii (optional): Defaults to True.

      • If set to True, it ensures that all non-ASCII characters in the generated JSON string are escaped to Unicode escape sequences (such as \uXXXX) to maintain the ASCII character set on output.
      • If it is set to False, non-ASCII characters will be retained in the output in their original form.
    • indent (optional): Used to specify the indentation level of the generated JSON string. If set to an integer, indentation will be included in the generated JSON string, making it easier to read.

    • skipkeys (optional): Defaults to False. If set to True, items whose dictionary keys are not primitive data types (str, int, float, bool, NoneType) are skipped. If the dictionary contains keys that are not basic data types, a TypeError will be raised.

    • check_circular (optional): Defaults to True.

      • If set to True, circular references are checked and a ValueError is raised.
      • If it is set to False, circular references are not checked.
    • allow_nan (optional): Defaults to True.

      • If set to True, this allows encoding floating-point values ​​into special values ​​such as NaN and Infinity, in compliance with the JSON specification.
      • If set to False, NaN and Infinity are treated as invalid input and a ValueError is raised.
    • cls (optional): Class used to specify a custom JSON encoder. Defaults to None, which means use the standard JSON encoder.

    • separators (optional): Used to specify separators in the generated JSON string.

      defaults to (", ",": "), which means that key-value pairs are separated by commas and colons. The separators can be customized as needed.

    import json
    
    str = '''
        [{
        "name":"AnJing",
        "age":"26"
    }]
    '''
    data = json.loads(str)
    print(data)				# 输出:[{'name': 'AnJing', 'age': '26'}]
    print(type(data))		# 输出:<class 'list'>
    print(data[0].get('name'))		# 输出:AnJing
    print(data[0]['name'])  		# 通过下标进行选择
    
    str_data = json.dumps(data)
    print(type(data))			# 输出:<class 'str'>
    print(data)					# 输出:[{"name": "AnJing", "age": "26"}]
    
  • load() : Read data from a JSON file and parse it into a Python object.

    It accepts one parameter: the file object to read.

  • dump() : Convert Python objects to JSON data and write them into file objects

    It accepts two parameters: the object to be serialized and the target file object

    Note: When using the dump and load functions, you need to provide a file object and ensure that the file is opened in the appropriate mode (e.g. ‘w’ for writing, ‘r’ for reading)

    import json
    
    str = [{
          
          
        "name":"AnJing",
        "age":"26"
    }]
    # 通过dump写入到文件中
    json.dump(str, open('123.json','w'))
    # 通过load查看是否写入
    data = json.load(open('123.json','r'))
    print(data)			# 输出:[{'name': 'AnJing', 'age': '26'}]
    

Common errors

  • Parse invalid JSON string

    If the provided JSON data is invalid, the parsing process will raise a JSONDecodeError exception.

    For example:

    • missing quotes

      invalid_json = '{"name": "ZhangSan", age: 30, "city": "ShenZhen"}'
      data = json.loads(invalid_json) 
      # 引发JSONDecodeError异常。原因为JSON字符串中的age缺少双引号,导致解析失败
      
    • Does not comply with JSON syntax rules (strings within JSON data must use double quotes)

  • Parsing a string with invalid attributes

    If the method input parameter is None or an empty string, the parsing process will raise an AttributeError error

  • Access a non-existent key

    If you try to access a key that does not exist in the JSON data, a KeyError exception will be raised.

    print(data['address'])  # 引发KeyError异常。原因为JSON数据中没有address键,访问该键将引发异常
    

Get the json string of the object excluding null fields

  • To convert an object containing a base model to JSON and remove its null fields, you can use the json module in Python and a recursive function to complete this task.

  • Sample code

    import json
    
    def remove_null(obj):
        if isinstance(obj, dict):
            return {
          
          key: remove_null(value) for key, value in obj.items() if value is not None}
        elif isinstance(obj, list):
            return [remove_null(item) for item in obj if item is not None]
        else:
            return obj
    
    # 示例对象,包含了一些 None 值
    data = {
          
          
        "name": "John",
        "age": None,
        "address": {
          
          
            "street": "123 Main St",
            "city": None,
            "zip_code": "98765"
        },
        "hobbies": ["reading", None, "swimming"]
    }
    # 使用 remove_null 函数去除 None 值
    data_without_null: dict = remove_null(data)
    # 将处理后的对象转换为 JSON 格式
    json_data = json.dumps(data_without_null)
    
    # 注意:继承自pydantic的BaseModel类的类,若有字段的类型也是BaseModel,需先转json再转dict,再传入函数
    json_data = json.dumps(remove_null(json.loads(base_model.josn())))
    

Guess you like

Origin blog.csdn.net/footless_bird/article/details/134639874