Python import json to file

Чтение и запись в файл JSON-объекта

Эта статья научит вас парсить данные из JSON. Также вы узнаете, как читать и записывать в файл данные JSON.

За последние 5-10 лет формат JSON был одним из самых популярных способов сериализации данных (если не самым популярным). Особенно в веб-разработке. С этим форматом вы столкнетесь при работе с REST API, конфигурациями приложений или базами данных.

Несомненно, знать принципы работы JSON — очень важно. В какой-то момент вы обязательно с ним встретитесь. Возможно, вы захотите узнать, как читать и записывать JSON в файл. Все эти действия — очень простые. В этом вы убедитесь, разобрав следующие примеры.

Запись JSON в файл

Самый простой способ записать JSON в файл — использовать словарь. Они могут хранить вложенные словари, массивы, булевы значения и другие типы данных вроде целых чисел и строк. Более детальный список поддерживаемых типов данных можно найти здесь.

Во встроенной библиотеке json есть «волшебный» метод, который позволяет конвертировать словари в сериализованную JSON-строку.

import json data = <> data['people'] = [] data['people'].append(< 'name': 'Scott', 'website': 'pythonist.ru', 'from': 'Nebraska' >) data['people'].append(< 'name': 'Larry', 'website': 'pythonist.ru', 'from': 'Michigan' >) data['people'].append(< 'name': 'Tim', 'website': 'pythonist.ru', 'from': 'Alabama' >) with open('data.txt', 'w') as outfile: json.dump(data, outfile)

После импорта библиотеки json мы объявляем несколько словарей и наполняем их данными. Самая важная часть — в конце программы. Здесь мы используем оператор with , чтобы открыть файл. После этого мы используем метод json.dump , чтобы записать наши словари в файл.

Вторым аргументом может быть любой файлоподобный объект — даже если это не совсем файл. Например, сокет. Его можно открыть, закрыть и записать так же, как и файл. С подобным вариантом использования JSON вы точно столкнетесь — это важно запомнить.

Стоит упомянуть и о вариации метода json.dump — json.dumps . Этот метод позволяет вернуть JSON-строку, а не записывать ее в файл. Это может быть полезно, если вы хотите изменить JSON-строку. (например, зашифровать)

Чтение JSON из файла

Чтение JSON из файла такое же простое, как и запись. С помощью библиотеки json мы можем спарсить JSON-строку прямо из файла. В этом примере мы парсим данные и выводим их в консоль:

import json with open('data.txt') as json_file: data = json.load(json_file) for p in data['people']: print('Name: ' + p['name']) print('Website: ' + p['website']) print('From: ' + p['from']) print('')

json.load — очень важный метод, запомните его. С его помощью происходит чтение файла, парс JSON-данных. После этого все данные записываются в словарь и возвращаются вам.

Как и у json.dump , у json.load есть дополнительный метод. Он позволяет работать со строками напрямую, ведь чаще всего у вас не будет файлоподобного объекта, содержащего JSON. Как вы уже догадались, называется он json.loads . Допустим, вы вызываете конечную точку REST с помощью GET, который возвращает строку. Ее мы и можем напрямую передать в json.loads .

Читайте также:  Jaxb exception in java

Параметры

При сериализации данных в JSON могут возникнуть проблемы. Например, его будет не очень удобно читать, ведь удаляются все пробелы. В большинстве случаев этот вариант вполне хорош, но порой нужно внести небольшие изменения. К примеру, добавить пробелы, чтобы JSON было удобнее читать. У json.load и json.dump есть несколько параметров, которые дают необходимую гибкость. О некоторых из них мы и поговорим.

Pretty-Printing

Сделать JSON более удобочитаемым (pretty-printing) — очень просто. Нужно лишь передать целое число в параметр indent :

import json data = <'people':[<'name': 'Scott', 'website': 'pythonist.ru', 'from': 'Nebraska'>]> json.dumps(data, indent=4) < "people": [ < "website": "pythonist.ru", "from": "Nebraska", "name": "Scott" >] >

Это довольно полезно. Особенно если вам часто приходится читать JSON во время работы. Также вы можете использовать использовать команду json.tool прямо в командной строке. Если вы хотите удобочитаемый JSON, наберите в командной строке следующий код:

Сортировка

В JSON объект определяется следующим образом:

Объект — это неупорядоченный набор пар ключ/значение.

То есть, порядок не гарантируется. Но навести его реально. Сделать это можно с помощью передачи True в параметр sort_keys в методах json.dump или json.dumps .

import json data = <'people':[<'name': 'Scott', 'website': 'pythonist.ru', 'from': 'Nebraska'>]> json.dumps(data, sort_keys=True, indent=4) < "people": [ < "from": "Nebraska", "name": "Scott", "website": "pythonist.ru" >] >

ASCII-текст

По умолчанию json.dump проверяет, имеет ли ваш текст в словаре кодировку ASCII. Если присутствуют символы, отличные от ASCII, они автоматически экранируются. Это показано в следующем примере:

import json data = jstr = json.dumps(data, indent=4) print(jstr)

Но это не всегда приемлемо. Во многих случаях вы бы хотели сохранить символы Unicode нетронутыми. Для этого нужно передать в параметр ensure_ascii значение False .

jstr = json.dumps(data, ensure_ascii=False, indent=4) print(jstr)

Источник

JSON in Python: How To Read, Write, and Parse

JSON in Python

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

Our premium courses offer a superior user experience with small, easy-to-digest lessons, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.

The Python Course for Beginners

Python Fundamentals II: Modules, Packages, Virtual Environments (2023)

NumPy Course: The Hands-on Introduction To NumPy (2023)

Learn more

This article is part of my Python tutorial. You can head over to the start of the tutorial here. You can navigate this tutorial using the buttons at the top and bottom of the articles. To get an overview of all articles in the tutorial, please use the fold-out menu at the top.

If you liked this article, you might also like to read the following articles:

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!

Источник

Оцените статью