JSON Dump

Python JSON Dump To Or Load From File Example

August 19, 2018 Reference : https://www.dev2qa.com/python-json-dump-to-or-load-from-file-example/

JSON is the standard data format that be used to save and transfer text data between programs. It is independent from programming language. Python provide built-in json module to process JSON string and Python object conversion. This article will give you some example.

1. JSON String And Python Object Conversion Overview.

Before you can use json module, you should import it first.

import json

1.1 Convert Dict Object To JSON String.

  1. Create a dict object.

  2. Call json.dumps() method to get JSON string from dict object.

    import json
    
    # Convert a python dict object to JSON string.
    def python_dict_to_json_string():
        # Create a dict object.
        dict_object = dict(name='Richard', age=25, score=100)
        # Call json.dumps method.
        json_string = json.dumps(dict_object)
        # Print JSON string in console.
        print(json_string)
    
    if __name__ == '__main__':
        python_dict_to_json_string()

    Below is the output.

    {"name": "Richard", "age": 25, "score": 100}

1.2 Convert JSON String To Dict Object.

  1. Create a JSON string.

  2. Call json.loads() method to return the python dict object.

Below is the output.

2. Dump Python Dict To JSON File Or Load Dict From JSON File.

2.1 Dump Dict To JSON File.

  1. Call open method to get a file object.

  2. Call json.dump(dict_object, file_object) to save dict data to JSON file.

When you execute above code, you can find the teacher_data.json file in current execution folder.

2.2 Load JSON File Data To Python Dict.

  1. Create a python file object by open method.

  2. Call json.load method to retrieve the JSON file content to a python dict object.

3. Python Class Instance Serialize And Deserialize.

Besides serialize/deserialize Python dict object between JSON string or file. We can also dump / load any Python class instance between JSON string or file.READ : How To Use User Defined Exception In Python

3.1 Dump Python Class Instance To JSON String.

  1. Create a python class.

  2. Add a method which can return a python dict object based on the class instance.

  1. Call json.dumps(class_instance, default=self.teacher_to_dict) method and set the convert method name in step 2 to parameter default. If do not set the default parameter value, it will throw TypeError: Object of type ‘Teacher’ is not JSON serializable error.

3.2 Load JSON String To Python Class Instance.

  1. Create a python class.

  2. Add a method which can return a class instance from loaded python dict object.

  1. Call json.loads(json_string, object_hook=self.dict_to_teacher) method and pass the method name in step 2 to the object_hook parameter.

3.3 Dump Python Class Instance To JSON File.

It is similar with steps in 3.1. The difference is you should call json.dump(class_instance, file_object, default=self.teacher_to_dict) method.

3.4 Load JSON File String To Python Class Instance.

It is similar with steps in 3.2. The difference is you should call json.load(file_object, object_hook=self.dict_to_teacher) method.

3.5 Python Class Instance And JSON Conversion Example.

Reference : https://www.dev2qa.com/python-json-dump-to-or-load-from-file-example/

Last updated

Was this helpful?