Python 101: An Intro to Working with JSON
https://ift.tt/2FHolZR
JavaScript Object Notation, more commonly known as JSON, is a lightweight data interchange format inspired by JavaScript object literal syntax. JSON is easy for humans to read and write. It is also easy for computers to parse and generate. JSON is used for storing and exchanging data in much the same way that XML is used.
Python has a built-in library called json
that you can use for creating, editing and parsing JSON. You can read all about this library here:
It would probably be helpful to know what JSON looks like. Here is an example of JSON from https://json.org:
{"menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } }}
From Python’s point of view, this JSON is a nested Python dictionary. You will find that JSON is always translated into some kind of native Python data type. In this article, you will learn about the following:
- Encoding a JSON String
- Decoding a JSON String
- Saving JSON to Disk
- Loading JSON from Disk
- Validating JSON with
json.tool
JSON is a very popular format that is often used in web applications. You will find that knowing how to interact with JSON using Python is useful in your own work.
Let’s get started!
Encoding a JSON String
Python’s json
module uses dumps()
to serialize an object to a string. The “s” in dumps()
stands for “string”. It’s easier to see how this works by using the json
module in some code:
>>> import json >>> j = {"menu": { ... "id": "file", ... "value": "File", ... "popup": { ... "menuitem": [ ... {"value": "New", "onclick": "CreateNewDoc()"}, ... {"value": "Open", "onclick": "OpenDoc()"}, ... {"value": "Close", "onclick": "CloseDoc()"} ... ] ... } ... }} >>> json.dumps(j) '{"menu": {"id": "file", "value": "File", "popup": {"menuitem": [{"value": "New", ' '"onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, ' '{"value": "Close", "onclick": "CloseDoc()"}]}}}'
Here you use json.dumps()
, which transforms the Python dictionary into a JSON string. The example’s output was modified to wrap the string for print. Otherwise the string would all be on one line.
Now you’re ready to learn how to write an object to disk!
Saving JSON to Disk
Python’s json
module uses the dump()
function to serialize or encode an object as a JSON formatted stream to a file-like object. File-like objects in Python are things like file handlers or objects that you create using Python’s io
module.
Go ahead and create a file named create_json_file.py
and add the following code to it:
# create_json_file.py import json def create_json_file(path, obj): with open(path, 'w') as fh: json.dump(obj, fh) if __name__ == '__main__': j = {"menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } }} create_json_file('test.json', j)
In this example, you use json.dump()
, which is for writing to a file or file-like object. It will write to the file-handler, fh
.
Now you can learn about decoding a JSON string!
Decoding a JSON String
Decoding or deserializing a JSON string is done via the loads()
method. loads()
is the companion function to dumps()
. Here is an example of its use:
>>> import json >>> j_str = """{"menu": { ... "id": "file", ... "value": "File", ... "popup": { ... "menuitem": [ ... {"value": "New", "onclick": "CreateNewDoc()"}, ... {"value": "Open", "onclick": "OpenDoc()"}, ... {"value": "Close", "onclick": "CloseDoc()"} ... ] ... } ... }} ... """ >>> j_obj = json.loads(j_str) >>> type(j_obj) <class 'dict'>
Here you recreate the JSON code from earlier as a Python multi-line string. Then you load the JSON string using json.loads()
, which converts it to a Python object. In this case, it converts the JSON to a Python dictionary.
Now you are ready to learn how to load JSON from a file!
Loading JSON from Disk
Loading JSON from a file is done using json.load()
. Here is an example:
# load_json_file.py import json def load_json_file(path): with open(path) as fh: j_obj = json.load(fh) print(type(j_obj)) if __name__ == '__main__': load_json_file('example.json')
In this code, you open the passed in file as you have seen before. Then you pass the file-handler, fh
, to json.load()
, which will transform the JSON into a Python object.
You can also use Python’s json
module to validate JSON. You will find out how to do that next.
Validating JSON with json.tool
Python’s json
module provides a tool you can run on the command line to check and see if the JSON has the correct syntax. Here are a couple of examples:
$ echo '{1.2:3.4}' | python -m json.tool Expecting property name enclosed in double quotes: line 1 column 2 (char 1) $ echo '{"1.2":3.4}' | python -m json.tool { "1.2": 3.4 }
The first call passes the string, '{1.2:3.4}'
to json.tool
, which tells you that there is something wrong with the JSON code. The second example shows you how to the fix the issue. When the fixed string is passed in to json.tool
, it will “pretty-print” the JSON back out instead of emitting an error.
Wrapping Up
The JSON format is used very often when working with web APIs and web frameworks. The Python language provides a nice tool for you to use to convert JSON to Python objects and back again in the json
library.
In this chapter, you learned about the following:
- Encoding a JSON String
- Decoding a JSON String
- Saving JSON to Disk
- Loading JSON from Disk
- Validating JSON with
json.tool
You now have another useful tool that you can use Python for. With a little practice, you will be working with JSON in no time!
The post Python 101: An Intro to Working with JSON appeared first on The Mouse Vs. The Python.
Python
via The Mouse Vs. The Python https://ift.tt/2m5EqsN
September 15, 2020 at 01:23AM