If you work with web technologies these days, there is no way around JSON. Today we look at the basic operations to serialize our objects to JSON and turn JSON back into objects.
This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.
JSON in Python
JSON (JavaScript Object Notation) is a lightweight data interchange format. In Python we can use the built-in json module in a similar way to pickle. We do not need to install anything and can start with importing json in our code:
1 |
import json |
Save as JSON
We can reuse the same example data from the post on PrettyPrinter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
def example(): # You collect and combine data in an # arbitrarily nested data structure: data = [ { 'name': "Rebecca Stephenson", 'phone': "(154) 221-8558", 'zipCode': "900185", 'country': "South Korea", 'options': ['a','b','c'], 'total': "$74.79" }, { 'name': "Amos Nieves", 'phone': "1-762-301-2264", 'zipCode': "25566", 'country': "Russian Federation", 'options': { 'a': 'full', 'f': 'partial', 'c': {'k1': 1, 'k2': 3} }, 'total': "$21.78" } ] return data |
We can create a JSON string with the dumps() method directly in-memory, without the need to write it to a file as we did with the similar method for pickle. I create a file only for the purpose of loading JSON in the next part:
1 2 3 4 5 6 |
def save(): data = example() data_json = json.dumps(data, indent=4) with open("data.json", "w") as f: f.write(data_json) |
This code creates a data.json file with this content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
[ { "name": "Rebecca Stephenson", "phone": "(154) 221-8558", "zipCode": "900185", "country": "South Korea", "options": [ "a", "b", "c" ], "total": "$74.79" }, { "name": "Amos Nieves", "phone": "1-762-301-2264", "zipCode": "25566", "country": "Russian Federation", "options": { "a": "full", "f": "partial", "c": { "k1": 1, "k2": 3 } }, "total": "$21.78" } ] |
If you want to change the separator or sort the keys, you can do that the same way as I did with the indentation on the method call for dumps().
Load from JSON
We can turn a JSON string back to objects with the loads() method:
1 2 3 4 5 |
def load(): with open("data.json", "r") as f: data_json = f.read() data = json.loads(data_json) print(data) |
If we run this code, we get this output:
1 |
[{‘name’: ‘Rebecca Stephenson’, ‘phone’: ‘(154) 221-8558’, ‘zipCode’: ‘900185’, ‘country’: ‘South Korea’, ‘options’: [‘a’, ‘b’, ‘c’], ‘total’: ‘$74.79’}, {‘name’: ‘Amos Nieves’, ‘phone’: ‘1-762-301-2264’, ‘zipCode’: ‘25566’, ‘country’: ‘Russian Federation’, ‘options’: {‘a’: ‘full’, ‘f’: ‘partial’, ‘c’: {‘k1’: 1, ‘k2’: 3}}, ‘total’: ‘$21.78’}] |
Conclusion
Working with JSON in Python is surprisingly painless and you do not need to install anything. All you need is a method call and your objects are in the JSON format.