- Работа с файлами в формате JSON#
- Чтение#
- json.load #
- json.loads #
- Запись#
- json.dumps #
- json.dump #
- Дополнительные параметры методов записи#
- Изменение типа данных#
- JSON in Python: How To Read, Write, and Parse
- Importing the built-in JSON library
- How to parse JSON in Python
- Encoding JSON with json.dumps
- Pretty printing JSON on the command line
- How to read a JSON file in python
- How to write JSON to a file in Python
- JSON5 vs. JSON
- Frequently Asked Questions
- Keep learning
- Get certified with our courses
- Leave a Comment Cancel reply
Работа с файлами в формате JSON#
JSON (JavaScript Object Notation) — это текстовый формат для хранения и обмена данными.
JSON по синтаксису очень похож на Python и достаточно удобен для восприятия.
Как и в случае с CSV, в Python есть модуль, который позволяет легко записывать и читать данные в формате JSON.
Чтение#
"access": [ "switchport mode access", "switchport access vlan", "switchport nonegotiate", "spanning-tree portfast", "spanning-tree bpduguard enable" ], "trunk": [ "switchport trunk encapsulation dot1q", "switchport mode trunk", "switchport trunk native vlan 999", "switchport trunk allowed vlan" ] >
Для чтения в модуле json есть два метода:
- json.load — метод считывает файл в формате JSON и возвращает объекты Python
- json.loads — метод считывает строку в формате JSON и возвращает объекты Python
json.load #
Чтение файла в формате JSON в объект Python (файл json_read_load.py):
import json with open('sw_templates.json') as f: templates = json.load(f) print(templates) for section, commands in templates.items(): print(section) print('\n'.join(commands))
$ python json_read_load.py access switchport mode access switchport access vlan switchport nonegotiate spanning-tree portfast spanning-tree bpduguard enable trunk switchport trunk encapsulation dot1q switchport mode trunk switchport trunk native vlan 999 switchport trunk allowed vlan
json.loads #
Считывание строки в формате JSON в объект Python (файл json_read_loads.py):
import json with open('sw_templates.json') as f: file_content = f.read() templates = json.loads(file_content) print(templates) for section, commands in templates.items(): print(section) print('\n'.join(commands))
Результат будет аналогичен предыдущему выводу.
Запись#
Запись файла в формате JSON также осуществляется достаточно легко.
Для записи информации в формате JSON в модуле json также два метода:
- json.dump — метод записывает объект Python в файл в формате JSON
- json.dumps — метод возвращает строку в формате JSON
json.dumps #
Преобразование объекта в строку в формате JSON (json_write_dumps.py):
import json trunk_template = [ 'switchport trunk encapsulation dot1q', 'switchport mode trunk', 'switchport trunk native vlan 999', 'switchport trunk allowed vlan' ] access_template = [ 'switchport mode access', 'switchport access vlan', 'switchport nonegotiate', 'spanning-tree portfast', 'spanning-tree bpduguard enable' ] to_json = 'trunk': trunk_template, 'access': access_template> with open('sw_templates.json', 'w') as f: f.write(json.dumps(to_json)) with open('sw_templates.json') as f: print(f.read())
Метод json.dumps подходит для ситуаций, когда надо вернуть строку в формате JSON. Например, чтобы передать ее API.
json.dump #
Запись объекта Python в файл в формате JSON (файл json_write_dump.py):
import json trunk_template = [ 'switchport trunk encapsulation dot1q', 'switchport mode trunk', 'switchport trunk native vlan 999', 'switchport trunk allowed vlan' ] access_template = [ 'switchport mode access', 'switchport access vlan', 'switchport nonegotiate', 'spanning-tree portfast', 'spanning-tree bpduguard enable' ] to_json = 'trunk': trunk_template, 'access': access_template> with open('sw_templates.json', 'w') as f: json.dump(to_json, f) with open('sw_templates.json') as f: print(f.read())
Когда нужно записать информацию в формате JSON в файл, лучше использовать метод dump.
Дополнительные параметры методов записи#
Методам dump и dumps можно передавать дополнительные параметры для управления форматом вывода.
По умолчанию эти методы записывают информацию в компактном представлении. Как правило, когда данные используются другими программами, визуальное представление данных не важно. Если же данные в файле нужно будет считать человеку, такой формат не очень удобно воспринимать.
К счастью, модуль json позволяет управлять подобными вещами.
Передав дополнительные параметры методу dump (или методу dumps), можно получить более удобный для чтения вывод (файл json_write_indent.py):
import json trunk_template = [ 'switchport trunk encapsulation dot1q', 'switchport mode trunk', 'switchport trunk native vlan 999', 'switchport trunk allowed vlan' ] access_template = [ 'switchport mode access', 'switchport access vlan', 'switchport nonegotiate', 'spanning-tree portfast', 'spanning-tree bpduguard enable' ] to_json = 'trunk': trunk_template, 'access': access_template> with open('sw_templates.json', 'w') as f: json.dump(to_json, f, sort_keys=True, indent=2) with open('sw_templates.json') as f: print(f.read())
Теперь содержимое файла sw_templates.json выглядит так:
"access": [ "switchport mode access", "switchport access vlan", "switchport nonegotiate", "spanning-tree portfast", "spanning-tree bpduguard enable" ], "trunk": [ "switchport trunk encapsulation dot1q", "switchport mode trunk", "switchport trunk native vlan 999", "switchport trunk allowed vlan" ] >
Изменение типа данных#
Еще один важный аспект преобразования данных в формат JSON: данные не всегда будут того же типа, что исходные данные в Python.
Например, кортежи при записи в JSON превращаются в списки:
In [1]: import json In [2]: trunk_template = ('switchport trunk encapsulation dot1q', . 'switchport mode trunk', . 'switchport trunk native vlan 999', . 'switchport trunk allowed vlan') In [3]: print(type(trunk_template)) In [4]: with open('trunk_template.json', 'w') as f: . json.dump(trunk_template, f, sort_keys=True, indent=2) . In [5]: cat trunk_template.json [ "switchport trunk encapsulation dot1q", "switchport mode trunk", "switchport trunk native vlan 999", "switchport trunk allowed vlan" ] In [6]: templates = json.load(open('trunk_template.json')) In [7]: type(templates) Out[7]: list In [8]: print(templates) ['switchport trunk encapsulation dot1q', 'switchport mode trunk', 'switchport trunk native vlan 999', 'switchport trunk allowed vlan']
Так происходит из-за того, что в JSON используются другие типы данных и не для всех типов данных Python есть соответствия.
Таблица конвертации данных Python в JSON:
JSON in Python: How To Read, Write, and Parse
JSON, short for JavaScript Object Notation, is an open standard. Although its name doesn’t imply so, it is a language-independent data format. With Python’s JSON library, we can read, write, and parse JSON to both store and exchange data using this versatile data format. It’s a prevalent data format because it is easy to read and write for humans as well, although not as easy as YAML!
Working with JSON in Python is super easy! Python has two data types that, together, form the perfect tool for working with JSON in Python: dictionaries and lists. In this article, I’ll show you how to use the built-in Python JSON library. In addition, we’ll take a look at JSON5: an extension to JSON that allows things like comments inside your JSON documents.
Importing the built-in JSON library
Python ships with a powerful and elegant JSON library to help you decode and encode JSON. You can import the module with:
This library is part of Python, so you don’t need to install it with the Pip package manager.
How to parse JSON in Python
Parsing a string of JSON data, also called decoding JSON, is as simple as using json.loads(…) . Loads is short for load string.
- objects to dictionaries
- arrays to lists,
- booleans, integers, floats, and strings are recognized for what they are and will be converted into the correct types in Python
- Any null will be converted into Python’s None type
Here’s an example of json.loads in action:
If the interactive example above doesn’t work (it’s still in beta), here’s a more static example instead:
>>> import json >>> jsonstring = » >>> person = json.loads(jsonstring) >>> print(person[‘name’], ‘is’, person[‘age’], ‘years old’) erik is 38 years old >>> print(person)
The output might look like a string, but it’s actually a dictionary that you can use in your code as explained on our page about Python dictionaries. You can check for yourself:
Encoding JSON with json.dumps
Encoding JSON data with Python’s json.dumps is just as easy as decoding. Use json.dumps (short for ‘dump to string’) to convert a Python object consisting of dictionaries, lists, and other native types into a string:
Here’s the same example, in case the above interactive example doesn’t work in your browser:
import json person = json_string = json.dumps(person) print(json_string) # # To make sure, let’s print the type too print(type(json_string)) #
This is the same document, converted back to a string! If you want to make your JSON document more readable for humans, use the indent option. It will nicely format the JSON, using space characters:
>>> person = >>> print(json.dumps(person, indent=2))
Pretty printing JSON on the command line
Python’s JSON module can also be used from the command line. It will both validate and pretty-print your JSON:
$ echo «< \"name\": \"Monty\", \"age\": 45 >» | \ python3 -m json.tool
You may also be interested in using the jq-tool for this though!
How to read a JSON file in python
Besides json.loads , there’s also a function called json.load (without the s). It will load data from a file, but you have to open the file yourself. If you want to read the contents of a JSON file into Python and parse it, use the following example:
with open('data.json') as json_file: data = json.load(json_file) .
How to write JSON to a file in Python
The json.dump function is used to write data to a JSON file. You’ll need to open the file in write mode first:
data = with open('data.json', 'w') as json_file: json.dump(data, json_file)
JSON5 vs. JSON
JSON5 is an extension of JSON. The main advantage of JSON5 over JSON is that it allows for more human-readable and editable JSON files. Notable JSON5 features are:
- single-line and multi-line comments
- trailing commas in objects and arrays
- single-quoted strings
For machine-to-machine communication, I recommend using the built-in JSON library. However, when using JSON as a configuration file, JSON5 is recommended, mainly because it allows for comments.
Python does not support JSON5 natively. To read and write JSON5, we’ll need to pip install one of the following packages:
- PyJSON5: a library that uses the official JSON5 C library, making it the fastest option to use.
- json5: a pure Python implementation, confusingly called pyjson5 as well on in their documentation. According to the author, the library is slow.
I recommend the first (fast) option, but unless you are parsing hundreds or thousands of documents, the speed advantage will be negligible.
Both libraries offer functions that mimic the Python JSON module, making it super easy to convert your code to JSON5. You could, for example, do an import pyjson5 as json but I recommend making it more explicit that you’re using json5 as show in the following example:
import pyjson5 as json5 with open("person.json") as f: p = json5.decode(f.read()) print(p)
To make it extra clear that you’re using JSON5, you can also use the extension .json5 . While you’re at it, search the marketplace of your code editor for a JSON5 plugin. E.g., VSCode has one or two.
Frequently Asked Questions
Simply use the methods described above. The json.dump and json.dumps functions accept both dictionaries and lists
Similar to arrays, so use json.dump or json.dumps on the dictionary.
The dump and dumps functions both accept an option called sort_keys, for example: json.dumps(data, sort_keys=True) .
By default: no. The library outputs ASCII and will convert characters that are not part of ASCII. If you want to output Unicode, set ensure_ascii to False. Example: json.dumps(data, ensure_ascii=False)
Keep learning
- If you’re looking for a format that is easy to write for humans (e.g.: config files), read our article on reading and writing YAML with Python.
- JMESPath is a query language for JSON. JMESPath in Python allows you to obtain the data you need from a JSON document or dictionary easily.
- If you need to parse JSON on the command-line, try our article on a tool called jq!
- Get a refresher on opening, writing, and reading files with Python.
Get certified with our courses
Learn Python properly through small, easy-to-digest lessons, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.
Leave a Comment Cancel reply
You must be logged in to post a comment.
You are browsing the free Python tutorial. Make sure to check out my full Python courses as well.
Subscribe to my newsletter for Python news, tips, and tricks!