Модуль json
Содержание:
- Performance
- Encoding¶
- Структура JSON
- Отладка файлов pickle
- 표준 준수와 상호 운용성¶
- Python NumPy
- Encoders and Decoders¶
- Understand the load and loads() method in detail
- Error reporting¶
- Сохранение данных в JSON
- Read JSON data from a file and convert it into dict using json.load()
- Object¶
- Infinite and NaN Numbers in Python
- Command Line Interface¶
- Command Line Interface¶
- Complex Object encoding of Python
- Interface en ligne de commande¶
Performance
Serialization and deserialization performance of orjson is better than
ultrajson, rapidjson, simplejson, or json. The benchmarks are done on
fixtures of real data:
-
twitter.json, 631.5KiB, results of a search on Twitter for «一», containing
CJK strings, dictionaries of strings and arrays of dictionaries, indented. -
github.json, 55.8KiB, a GitHub activity feed, containing dictionaries of
strings and arrays of dictionaries, not indented. -
citm_catalog.json, 1.7MiB, concert data, containing nested dictionaries of
strings and arrays of integers, indented. -
canada.json, 2.2MiB, coordinates of the Canadian border in GeoJSON
format, containing floats and arrays, indented.
Latency
twitter.json serialization
Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
---|---|---|---|
orjson | 0.59 | 1698.8 | 1 |
ujson | 2.14 | 464.3 | 3.64 |
rapidjson | 2.39 | 418.5 | 4.06 |
simplejson | 3.15 | 316.9 | 5.36 |
json | 3.56 | 281.2 | 6.06 |
twitter.json deserialization
Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
---|---|---|---|
orjson | 2.28 | 439.3 | 1 |
ujson | 2.89 | 345.9 | 1.27 |
rapidjson | 3.85 | 259.6 | 1.69 |
simplejson | 3.66 | 272.1 | 1.61 |
json | 4.05 | 246.7 | 1.78 |
github.json serialization
Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
---|---|---|---|
orjson | 0.07 | 15265.2 | 1 |
ujson | 0.22 | 4556.7 | 3.35 |
rapidjson | 0.26 | 3808.9 | 4.02 |
simplejson | 0.37 | 2690.4 | 5.68 |
json | 0.35 | 2847.8 | 5.36 |
github.json deserialization
Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
---|---|---|---|
orjson | 0.18 | 5610.1 | 1 |
ujson | 0.28 | 3540.7 | 1.58 |
rapidjson | 0.33 | 3031.5 | 1.85 |
simplejson | 0.29 | 3385.6 | 1.65 |
json | 0.29 | 3402.1 | 1.65 |
citm_catalog.json serialization
Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
---|---|---|---|
orjson | 0.99 | 1008.5 | 1 |
ujson | 3.69 | 270.7 | 3.72 |
rapidjson | 3.55 | 281.4 | 3.58 |
simplejson | 11.76 | 85.1 | 11.85 |
json | 6.89 | 145.1 | 6.95 |
citm_catalog.json deserialization
Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
---|---|---|---|
orjson | 4.53 | 220.5 | 1 |
ujson | 5.67 | 176.5 | 1.25 |
rapidjson | 7.51 | 133.3 | 1.66 |
simplejson | 7.54 | 132.7 | 1.66 |
json | 7.8 | 128.2 | 1.72 |
canada.json serialization
Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
---|---|---|---|
orjson | 4.72 | 198.9 | 1 |
ujson | 17.76 | 56.3 | 3.77 |
rapidjson | 61.83 | 16.2 | 13.11 |
simplejson | 80.6 | 12.4 | 17.09 |
json | 52.38 | 18.8 | 11.11 |
canada.json deserialization
Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
---|---|---|---|
orjson | 10.28 | 97.4 | 1 |
ujson | 16.49 | 60.5 | 1.6 |
rapidjson | 37.92 | 26.4 | 3.69 |
simplejson | 37.7 | 26.5 | 3.67 |
json | 37.87 | 27.6 | 3.68 |
Memory
orjson’s memory usage when deserializing is similar to or lower than
the standard library and other third-party libraries.
This measures, in the first column, RSS after importing a library and reading
the fixture, and in the second column, increases in RSS after repeatedly
calling on the fixture.
Library | import, read() RSS (MiB) | loads() increase in RSS (MiB) |
---|---|---|
orjson | 13.5 | 2.5 |
ujson | 14 | 4.1 |
rapidjson | 14.7 | 6.5 |
simplejson | 13.2 | 2.5 |
json | 12.9 | 2.3 |
github.json
Library | import, read() RSS (MiB) | loads() increase in RSS (MiB) |
---|---|---|
orjson | 13.1 | 0.3 |
ujson | 13.5 | 0.3 |
rapidjson | 14 | 0.7 |
simplejson | 12.6 | 0.3 |
json | 12.3 | 0.1 |
citm_catalog.json
Library | import, read() RSS (MiB) | loads() increase in RSS (MiB) |
---|---|---|
orjson | 14.6 | 7.9 |
ujson | 15.1 | 11.1 |
rapidjson | 15.8 | 36 |
simplejson | 14.3 | 27.4 |
json | 14 | 27.2 |
canada.json
Library | import, read() RSS (MiB) | loads() increase in RSS (MiB) |
---|---|---|
orjson | 17.1 | 15.7 |
ujson | 17.6 | 17.4 |
rapidjson | 18.3 | 17.9 |
simplejson | 16.9 | 19.6 |
json | 16.5 | 19.4 |
Reproducing
The above was measured using Python 3.8.3 on Linux (x86_64) with
orjson 3.3.0, ujson 3.0.0, python-rapidson 0.9.1, and simplejson 3.17.2.
The latency results can be reproduced using the and
scripts. The memory results can be reproduced using the script.
Encoding¶
This sections describes the functions that can be used to encode
values to JSON. By default, only objects and arrays can be encoded
directly, since they are the only valid root values of a JSON text.
To encode any JSON value, use the JSON_ENCODE_ANY flag (see
below).
By default, the output has no newlines, and spaces are used between
array and object elements for a readable output. This behavior can be
altered by using the JSON_INDENT and JSON_COMPACT flags
described below. A newline is never appended to the end of the encoded
JSON data.
Each function takes a flags parameter that controls some aspects of
how the data is encoded. Its default value is 0. The following macros
can be ORed together to obtain flags.
- JSON_INDENT(n)
- Pretty-print the result, using newlines between array and object
items, and indenting with n spaces. The valid range for n is
between 0 and 31 (inclusive), other values result in an undefined
output. If JSON_INDENT is not used or n is 0, no newlines are
inserted between array and object items. - JSON_COMPACT
- This flag enables a compact representation, i.e. sets the separator
between array and object items to "," and between object keys
and values to ":". Without this flag, the corresponding
separators are ", " and ": " for more readable output. - JSON_ENSURE_ASCII
- If this flag is used, the output is guaranteed to consist only of
ASCII characters. This is achived by escaping all Unicode
characters outside the ASCII range. - JSON_SORT_KEYS
- If this flag is used, all the objects in output are sorted by key.
This is useful e.g. if two JSON texts are diffed or visually
compared. - JSON_PRESERVE_ORDER
- If this flag is used, object keys in the output are sorted into the
same order in which they were first inserted to the object. For
example, decoding a JSON text and then encoding with this flag
preserves the order of object keys. - JSON_ENCODE_ANY
-
Specifying this flag makes it possible to encode any JSON value on
its own. Without it, only objects and arrays can be passed as the
root value to the encoding functions.Note: Encoding any value may be useful in some scenarios, but
it’s generally discouraged as it violates strict compatiblity with
RFC 4627. If you use this flag, don’t expect interoperatibility
with other JSON systems. Even Jansson itself doesn’t have any means
to decode JSON texts whose root value is not object or array.New in version 2.1.
The following functions perform the actual JSON encoding. The result
is in UTF-8.
- char *json_dumps(const *root, size_t flags)
-
Returns the JSON representation of root as a string, or NULL on
error. flags is described above. The return value must be freed
by the caller using free().
- int json_dumpf(const *root, FILE *output, size_t flags)
-
Write the JSON representation of root to the stream output.
flags is described above. Returns 0 on success and -1 on error.
If an error occurs, something may have already been written to
output. In this case, the output is undefined and most likely not
valid JSON.
- int json_dump_file(const *json, const char *path, size_t flags)
-
Write the JSON representation of root to the file path. If
path already exists, it is overwritten. flags is described
above. Returns 0 on success and -1 on error.
- json_dump_callback_t
-
A typedef for a function that’s called by
:typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);
buffer points to a buffer containing a chunk of output, size is
the length of the buffer, and data is the corresponding
argument passed through.On error, the function should return -1 to stop the encoding
process. On success, it should return 0.New in version 2.2.
Структура JSON
Готовьтесь. Я собираюсь показать реальный пример JSON— такой же, какой вы встретите в реальной жизни. Это нормально, подразумевается что JSON является читаемым для любого, кто пользовался С-языками, а Python – это С-язык, так что мы говорим о вас!
Python
{
«firstName»: «Jane»,
«lastName»: «Doe»,
«hobbies»: ,
«age»: 35,
«children»:
}
1 |
{ «firstName»»Jane», «lastName»»Doe», «hobbies»»running»,»sky diving»,»singing», «age»35, «children» { «firstName»»Alice», «age»6 }, { «firstName»»Bob», «age»8 } } |
Как видите, JSON поддерживает примитивные типы, такие как строки python и числа, а также вложенные и объекты.
НУ что же, вы пережили первый контакт с диким JSON. Теперь вам нужно научиться приручать его!
Отладка файлов pickle
Как выглядит протокол pickle? Давайте ненадолго отложим консоль python и взглянем в файл entry.pickle, который мы создали. Для не вооруженного взгляда он выглядит как тарабарщина.
you@localhost:~diveintopython3examples$ ls -l entry.pickle-rw-r—r— 1 you you 358 Aug 3 13:34 entry.pickle
you@localhost:~diveintopython3examples$ cat entry.pickle
comments_linkqNXtagsqXdiveintopythonqXdocbookqXhtmlq?qX publishedq?
XlinkXJhttp://diveintomark.orgarchives20090327dive-into-history-2009-edition
q Xpublished_dateq
ctime
struct_time
?qRqXtitleqXDive into history, 2009 editionqu.
Не слишком то полезно. Вы можете видеть строки, но остальные типы данных выглядят как непечатаемые (или как минимум не читаемые) символы. Поля даже не разделены хотя бы табуляцией или пробелами. Это не тот формат, который вы бы захотели отлаживать вручную.
>>> shell1>>> import pickletools>>> with open(‘entry.pickle’, ‘rb’) as f:
… pickletools.dis(f)
: \x80 PROTO 3
2: } EMPTY_DICT
3: q BINPUT
5: ( MARK
6: X BINUNICODE ‘published_date’
25: q BINPUT 1
27: c GLOBAL ‘time struct_time’
45: q BINPUT 2
47: ( MARK
48: M BININT2 2009
51: K BININT1 3
53: K BININT1 27
55: K BININT1 22
57: K BININT1 20
59: K BININT1 42
61: K BININT1 4
63: K BININT1 86
65: J BININT -1
70: t TUPLE (MARK at 47)
71: q BINPUT 3
73: } EMPTY_DICT
74: q BINPUT 4
76: \x86 TUPLE2
77: q BINPUT 5
79: R REDUCE
80: q BINPUT 6
82: X BINUNICODE ‘comments_link’
100: q BINPUT 7
102: N NONE
103: X BINUNICODE ‘internal_id’
119: q BINPUT 8
121: C SHORT_BINBYTES ‘ÞÕ´ø’
127: q BINPUT 9
129: X BINUNICODE ‘tags’
138: q BINPUT 10
140: X BINUNICODE ‘diveintopython’
159: q BINPUT 11
161: X BINUNICODE ‘docbook’
173: q BINPUT 12
175: X BINUNICODE ‘html’
184: q BINPUT 13
186: \x87 TUPLE3
187: q BINPUT 14
189: X BINUNICODE ‘title’
199: q BINPUT 15
201: X BINUNICODE ‘Dive into history, 2009 edition’
237: q BINPUT 16
239: X BINUNICODE ‘article_link’
256: q BINPUT 17
258: X BINUNICODE ‘http://diveintomark.org/archives/2009/03/27/dive-into-history-2009-edition’
337: q BINPUT 18
339: X BINUNICODE ‘published’
353: q BINPUT 19
355: \x88 NEWTRUE
356: u SETITEMS (MARK at 5)
357: . STOP
highest protocol among opcodes = 3
Самая интересная часть информации в дизассемблере находится на последней строке, потому что она включает версию протокола, при помощи которого данный файл был сохранен. Не существует явного маркера протокола pickle. Чтобы определить какую версию протокола использовали для сохранения фала Pickle, вам необходимо заглянуть в маркеры(«opcodes») внутри сохраненных данных и использовать вшитую информацию о том какие маркеры были введены, в какой версии протокола Pickle. Функция pickletools.dis() делает именно это, и она печатает результат в последней строке дизассемблированного вывода. Вот функция, которая возвращает только номер версии, без вывода данных:
import pickletoolsdef protocol_version(file_object):
maxproto = -1
for opcode, arg, pos in pickletools.genops(file_object):
maxproto = max(maxproto, opcode.proto)
return maxproto
И вот она же в действии:>>> import pickleversion>>> with open(‘entry.pickle’, ‘rb’) as f:
… v = pickleversion.protocol_version(f)>>> v3
표준 준수와 상호 운용성¶
JSON 형식은 RFC 7159와 ECMA-404에 의해 지정됩니다. 이 절에서는 이 모듈의 RFC 준수 수준에 대해 자세히 설명합니다. 단순화를 위해, 및 서브 클래스와 명시적으로 언급되지 않은 매개 변수는 고려되지 않습니다.
유효한 JavaScript이지만 유효한 JSON이 아닌 확장을 구현함으로써, 이 모듈은 엄격한 방식으로 RFC를 준수하지는 않습니다. 특히:
-
무한대와 NaN 숫자 값이 받아들여지고 출력됩니다;
-
오브젝트 내에서 반복되는 이름이 허용되고, 마지막 이름-값 쌍의 값만 사용됩니다.
RFC가 RFC를 준수하는 구문 분석기가 RFC를 준수하지 않는 입력 텍스트를 받아들이도록 허용하기 때문에, 이 모듈의 역 직렬화기는 기본 설정에서 기술적으로 RFC를 준수합니다.
문자 인코딩
RFC는 UTF-8, UTF-16 또는 UTF-32를 사용하여 JSON을 표현할 것을 요구하고, 최대 상호 운용성을 위해 권장되는 기본값은 UTF-8입니다.
RFC에 의해 요구되는 것은 아니지만 허용되기 때문에, 이 모듈의 직렬화기는 기본적으로 ensure_ascii=True를 설정하므로, 결과 문자열에 ASCII 문자만 포함되도록 출력을 이스케이핑 합니다.
ensure_ascii 매개 변수 외에도, 이 모듈은 파이썬 객체와 사이의 변환으로 엄격하게 정의되어 있으므로, 문자 인코딩 문제를 직접 다루지 않습니다.
RFC는 JSON 텍스트의 시작 부분에 바이트 순서 표시(BOM)를 추가하는 것을 금지하고 있으며, 이 모듈의 직렬화기는 BOM을 출력에 추가하지 않습니다. RFC는 JSON 역 직렬화기가 입력에서 초기 BOM을 무시하는 것을 허용하지만 요구하지는 않습니다. 이 모듈의 역 직렬화기는 초기 BOM이 있을 때 를 발생시킵니다.
RFC는 유효한 유니코드 문자에 해당하지 않는 바이트 시퀀스(예를 들어, 쌍을 이루지 않은 UTF-16 대리 코드(unpaired UTF-16 surrogates))가 포함된 JSON 문자열을 명시적으로 금지하지 않지만, 상호 운용성 문제를 일으킬 수 있다고 지적하고 있습니다. 기본적으로, 이 모듈은 이러한 시퀀스의 코드 포인트를 받아들이고 (원래 에 있을 때) 출력합니다.
무한대와 NaN 숫자 값
RFC는 무한대나 NaN 숫자 값의 표현을 허용하지 않습니다. 그런데도, 기본적으로, 이 모듈은 유효한 JSON 숫자 리터럴 값인 것처럼 , 및 을 받아들이고 출력합니다:
>>> # Neither of these calls raises an exception, but the results are not valid JSON >>> json.dumps(float('-inf')) '-Infinity' >>> json.dumps(float('nan')) 'NaN' >>> # Same when deserializing >>> json.loads('-Infinity') -inf >>> json.loads('NaN') nan
직렬화기에서, allow_nan 매개 변수를 사용하여 이 동작을 변경할 수 있습니다. 역 직렬화기에서, parse_constant 매개 변수를 사용하여 이 동작을 변경할 수 있습니다.
오브젝트 내에서 반복된 이름
RFC는 JSON 오브젝트 내에서 이름이 고유해야 한다고 지정하지만, JSON 오브젝트 내에서 반복되는 이름을 처리하는 방법을 지정하지는 않습니다. 기본적으로, 이 모듈은 예외를 발생시키지 않습니다; 대신, 주어진 이름에 대한 마지막 이름-값 쌍을 제외한 모든 것을 무시합니다:
>>> weird_json = '{"x": 1, "x": 2, "x": 3}' >>> json.loads(weird_json) {'x': 3}
object_pairs_hook 매개 변수는 이 동작을 변경하는 데 사용할 수 있습니다.
오브젝트나 배열이 아닌 최상윗값
폐지된 RFC 4627에 의해 지정된 이전 버전의 JSON은 JSON 텍스트의 최상윗값이 JSON 오브젝트나 배열(파이썬 나 )이어야 하고, JSON null, 불리언, 숫자 또는 문자열 값이 될 수 없다고 요구합니다. RFC 7159는 그 제한을 제거했으며, 이 모듈은 직렬화기와 역 직렬화기에서 이러한 제한을 구현하지 않으며, 그런 적도 없습니다.
이와 관계없이, 최대한의 상호 운용성을 위해, 여러분은 자발적으로 제한을 준수하기를 원할 수 있습니다.
Python NumPy
NumPy IntroNumPy Getting StartedNumPy Creating ArraysNumPy Array IndexingNumPy Array SlicingNumPy Data TypesNumPy Copy vs ViewNumPy Array ShapeNumPy Array ReshapeNumPy Array IteratingNumPy Array JoinNumPy Array SplitNumPy Array SearchNumPy Array SortNumPy Array FilterNumPy Random
Random Intro
Data Distribution
Random Permutation
Seaborn Module
Normal Distribution
Binomial Distribution
Poisson Distribution
Uniform Distribution
Logistic Distribution
Multinomial Distribution
Exponential Distribution
Chi Square Distribution
Rayleigh Distribution
Pareto Distribution
Zipf Distribution
NumPy ufunc
ufunc Intro
ufunc Create Function
ufunc Simple Arithmetic
ufunc Rounding Decimals
ufunc Logs
ufunc Summations
ufunc Products
ufunc Differences
ufunc Finding LCM
ufunc Finding GCD
ufunc Trigonometric
ufunc Hyperbolic
ufunc Set Operations
Encoders and Decoders¶
- class (*, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=None)
-
Simple JSON decoder.
Performs the following translations in decoding by default:
JSON
Python
object
dict
array
list
string
str
number (int)
int
number (real)
float
true
True
false
False
null
None
It also understands , , and as their
corresponding values, which is outside the JSON spec.object_hook, if specified, will be called with the result of every JSON
object decoded and its return value will be used in place of the given
. This can be used to provide custom deserializations (e.g. to
support JSON-RPC class hinting).object_pairs_hook, if specified will be called with the result of every
JSON object decoded with an ordered list of pairs. The return value of
object_pairs_hook will be used instead of the . This
feature can be used to implement custom decoders. If object_hook is also
defined, the object_pairs_hook takes priority.Changed in version 3.1: Added support for object_pairs_hook.
parse_float, if specified, will be called with the string of every JSON
float to be decoded. By default, this is equivalent to .
This can be used to use another datatype or parser for JSON floats
(e.g. ).parse_int, if specified, will be called with the string of every JSON int
to be decoded. By default, this is equivalent to . This can
be used to use another datatype or parser for JSON integers
(e.g. ).parse_constant, if specified, will be called with one of the following
strings: , , .
This can be used to raise an exception if invalid JSON numbers
are encountered.If strict is false ( is the default), then control characters
will be allowed inside strings. Control characters in this context are
those with character codes in the 0–31 range, including (tab),
, and .If the data being deserialized is not a valid JSON document, a
will be raised.Changed in version 3.6: All parameters are now .
- (s)
-
Return the Python representation of s (a instance
containing a JSON document).will be raised if the given JSON document is not
valid.
- (s)
-
Decode a JSON document from s (a beginning with a
JSON document) and return a 2-tuple of the Python representation
and the index in s where the document ended.This can be used to decode a JSON document from a string that may have
extraneous data at the end.
Understand the load and loads() method in detail
We can do many JSON parsing operations using load and loads() method. First, understand it’s syntax and arguments, then we move to its usage one-by-one.
All arguments have the same meaning in both methods.
The is used to read the JSON document from file and The is used to convert the JSON String document into the Python dictionary.
- file pointer used to read a text file, binary file or a JSON file that contains a JSON document.
- is the optional function that will be called with the result of any object literal decoded. The Python built-in json module can only handle primitives types that have a direct JSON equivalent (e.g., dictionary, lists, strings, Numbers, None, etc.). But when you want to convert JSON data into a custom Python type, we need to implement custom decoder and pass it as an object to a method so we can get a custom Python type in return instead of a dictionary.
- is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value of will be used instead of the Python dictionary. This feature can also be used to implement custom decoders. If is also defined, the takes priority.
- is optional parameters but, if specified, will be called with the string of every JSON float and integer to be decoded. By default, this is equivalent to .
- if specified, it will be called with the string of every JSON int to be decoded. By default, this is equivalent to .
We will see the use of all these parameters in detail.
Error reporting¶
Jansson uses a single struct type to pass error information to the
user. See sections , and
for functions that pass error information using
this struct.
- json_error_t
-
- char text[]
-
The error message (in UTF-8), or an empty string if a message is
not available.
- char source[]
-
Source of the error. This can be (a part of) the file name or a
special identifier in angle brackers (e.g. <string>).
- int line
-
The line number on which the error occurred.
- int column
-
The column on which the error occurred. Note that this is the
character column, not the byte column, i.e. a multibyte UTF-8
character counts as one column.
- size_t position
-
The position in bytes from the start of the input. This is
useful for debugging Unicode encoding problems.
The normal use of is to allocate it on the stack,
and pass a pointer to a function. Example:
int main() { json_t *json; json_error_t error; json = json_load_file("/path/to/file.json", , &error); if(!json) { /* the error variable contains error information */ } ... }
Also note that if the call succeeded (json != NULL in the above
example), the contents of error are unspecified.
Сохранение данных в JSON
Чтобы записать информацию в формате JSON с помощью средств языка Python, нужно прежде всего подключить модуль json, воспользовавшись командой import json в начале файла с кодом программы. Метод dumps отвечает за автоматическую упаковку данных в JSON, принимая при этом переменную, которая содержит всю необходимую информацию. В следующем примере демонстрируется кодирование словаря под названием dictData. В нем имеются некие сведения о пользователе интернет-портала, такие как идентификационный код, логин, пароль, полное имя, номер телефона, адрес электронной почты и данные об активности. Эти значения представлены в виде обычных строк, а также целых чисел и булевых литералов True/False.
import json dictData = { "ID" : 210450, "login" : "admin", "name" : "John Smith", "password" : "root", "phone" : 5550505, "email" : "smith@mail.com", "online" : True } jsonData = json.dumps(dictData) print(jsonData) {"ID": 210450, "login": "admin", "name": "John Smith", "password": "root", "phone": 5550505, "email": "smith@mail.com", "online": true}
Результат выполнения метода dumps передается в переменную под названием jsonData. Таким образом, словарь dictData был преобразован в JSON-формат всего одной строчкой. Как можно увидеть, благодаря функции print, все сведения были закодированы в своем изначальном виде. Стоит заметить, что данные из поля online были преобразованы из литерала True в true.
С помощью Python сделаем запись json в файл. Для этого дополним код предыдущего примера следующим образом:
with open("data.json", "w") as file: file.write(jsonData)
Подробнее про запись данных в текстовые файлы описано в отдельной статье на нашем сайте.
Read JSON data from a file and convert it into dict using json.load()
Using a method, we can read JSON data from text, JSON, or binary file. The method returns data in the form of a Python dictionary. Later we use this dictionary to access and manipulate data in our application or system.
Now, let’s see the example. For this example, I am reading the “developer.json” file present on my hard drive. This file contains the following JSON data.
{ "name": "jane doe", "salary": 9000, "skills": , "email": "JaneDoe@pynative.com", "projects": }
Developer JSON file
Output:
Started Reading JSON file Converting JSON encoded data into Python dictionary Decoded JSON Data From File name : jane doe salary : 9000 skills : email : JaneDoe@pynative.com projects : Done reading json file
Access JSON data directly using key name
Use the following code If you want to access the JSON key directly instead of iterating the entire JSON from a file
Output:
Started Reading JSON file Converting JSON encoded data into Python dictionary Decoding JSON Data From File Printing JSON values using key jane doe 9000 JaneDoe@pynative.com Done reading json file
You can read the JSON data from text, json, or a binary file using the same way mentioned above.
Object¶
A JSON object is a dictionary of key-value pairs, where the key is a
Unicode string and the value is any JSON value.
- *json_object(void)
-
Return value: New reference.
Returns a new JSON object, or NULL on error. Initially, the
object is empty.
- size_t json_object_size(const *object)
-
Returns the number of elements in object, or 0 if object is not
a JSON object.
- *json_object_get(const *object, const char *key)
-
Return value: Borrowed reference.
Get a value corresponding to key from object. Returns NULL if
key is not found and on error.
- int json_object_set( *object, const char *key, *value)
-
Set the value of key to value in object. key must be a
valid null terminated UTF-8 encoded Unicode string. If there
already is a value for key, it is replaced by the new value.
Returns 0 on success and -1 on error.
- int json_object_set_nocheck( *object, const char *key, *value)
-
Like , but doesn’t check that key is
valid UTF-8. Use this function only if you are certain that this
really is the case (e.g. you have already checked it by other
means).
- int json_object_set_new( *object, const char *key, *value)
-
Like but steals the reference to
value. This is useful when value is newly created and not used
after the call.
- int json_object_set_new_nocheck( *object, const char *key, *value)
-
Like , but doesn’t check that key is
valid UTF-8. Use this function only if you are certain that this
really is the case (e.g. you have already checked it by other
means).
- int json_object_del( *object, const char *key)
-
Delete key from object if it exists. Returns 0 on success, or
-1 if key was not found. The reference count of the removed value
is decremented.
- int json_object_clear( *object)
-
Remove all elements from object. Returns 0 on success and -1 if
object is not a JSON object. The reference count of all removed
values are decremented.
- int json_object_update( *object, *other)
-
Update object with the key-value pairs from other, overwriting
existing keys. Returns 0 on success or -1 on error.
The following functions implement an iteration protocol for objects,
allowing to iterate through all key-value pairs in an object. The
items are not returned in any particular order, as this would require
sorting due to the internal hashtable implementation.
- void *json_object_iter( *object)
-
Returns an opaque iterator which can be used to iterate over all
key-value pairs in object, or NULL if object is empty.
- void *json_object_iter_at( *object, const char *key)
-
Like , but returns an iterator to the
key-value pair in object whose key is equal to key, or NULL if
key is not found in object. Iterating forward to the end of
object only yields all key-value pairs of the object if key
happens to be the first key in the underlying hash table.
- void *json_object_iter_next( *object, void *iter)
-
Returns an iterator pointing to the next key-value pair in object
after iter, or NULL if the whole object has been iterated
through.
- const char *json_object_iter_key(void *iter)
-
Extract the associated key from iter.
- *json_object_iter_value(void *iter)
-
Return value: Borrowed reference.
Extract the associated value from iter.
- int json_object_iter_set( *object, void *iter, *value)
-
Set the value of the key-value pair in object, that is pointed to
by iter, to value.
- int json_object_iter_set_new( *object, void *iter, *value)
-
Like , but steals the reference to
value. This is useful when value is newly created and not used
after the call.
The iteration protocol can be used for example as follows:
Infinite and NaN Numbers in Python
JSON Data Interchange Format (RFC – Request For Comments) doesn’t allow Infinite or Nan Value but there is no restriction in Python- JSON Library to perform Infinite and Nan Value related operation. If JSON gets INFINITE and Nan datatype than it’s converted it into literal.
Example,
import json # pass float Infinite value infinite_json = json.dumps(float('inf')) # check infinite json type print(infinite_json) print(type(infinite_json)) json_nan = json.dumps(float('nan')) print(json_nan) # pass json_string as Infinity infinite = json.loads('Infinity') print(infinite) # check type of Infinity print(type(infinite))
Output:
Infinity <class 'str'> NaN inf <class 'float'>
Command Line Interface¶
Source code: Lib/json/tool.py
The module provides a simple command line interface to validate
and pretty-print JSON objects.
If the optional and arguments are not
specified, and will be used respectively:
$ echo '{"json": "obj"}' | python -m json.tool { "json": "obj" } $ echo '{1.2:3.4}' | python -m json.tool Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Changed in version 3.5: The output is now in the same order as the input. Use the
option to sort the output of dictionaries
alphabetically by key.
Command line options
-
The JSON file to be validated or pretty-printed:
$ python -m json.tool mp_films.json { "title": "And Now for Something Completely Different", "year": 1971 }, { "title": "Monty Python and the Holy Grail", "year": 1975 }
If infile is not specified, read from .
-
Write the output of the infile to the given outfile. Otherwise, write it
to .
-
Sort the output of dictionaries alphabetically by key.
New in version 3.5.
-
Disable escaping of non-ascii characters, see for more information.
New in version 3.9.
-
Parse every input line as separate JSON object.
New in version 3.8.
-
Mutually exclusive options for whitespace control.
New in version 3.9.
-
Show the help message.
Footnotes
-
As noted in the errata for RFC 7159,
JSON permits literal U+2028 (LINE SEPARATOR) and
U+2029 (PARAGRAPH SEPARATOR) characters in strings, whereas JavaScript
(as of ECMAScript Edition 5.1) does not.
Command Line Interface¶
Source code: Lib/json/tool.py
The module provides a simple command line interface to validate
and pretty-print JSON objects.
If the optional and arguments are not
specified, and will be used respectively:
$ echo '{"json": "obj"}' | python -m json.tool { "json": "obj" } $ echo '{1.2:3.4}' | python -m json.tool Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Changed in version 3.5: The output is now in the same order as the input. Use the
option to sort the output of dictionaries
alphabetically by key.
Command line options
-
The JSON file to be validated or pretty-printed:
$ python -m json.tool mp_films.json { "title": "And Now for Something Completely Different", "year": 1971 }, { "title": "Monty Python and the Holy Grail", "year": 1975 }
If infile is not specified, read from .
-
Write the output of the infile to the given outfile. Otherwise, write it
to .
-
Sort the output of dictionaries alphabetically by key.
New in version 3.5.
-
Show the help message.
Footnotes
-
As noted in the errata for RFC 7159,
JSON permits literal U+2028 (LINE SEPARATOR) and
U+2029 (PARAGRAPH SEPARATOR) characters in strings, whereas JavaScript
(as of ECMAScript Edition 5.1) does not.
Complex Object encoding of Python
A Complex object has two different parts that is
- Real part
- Imaginary part
Example: 3 +2i
Before performing encoding of a complex object, you need to check a variable is complex or not. You need to create a function which checks the value stored in a variable by using an instance method.
Let’s create the specific function for check object is complex or eligible for encoding.
import json # create function to check instance is complex or not def complex_encode(object): # check using isinstance method if isinstance(object, complex): return # raised error using exception handling if object is not complex raise TypeError(repr(object) + " is not JSON serialized") # perform json encoding by passing parameter complex_obj = json.dumps(4 + 5j, default=complex_encode) print(complex_obj)
Output:
''
Interface en ligne de commande¶
Code source : Lib/json/tool.py
Le module fournit une simple interface en ligne de commande pour valider et réécrire élégamment des objets JSON.
Si les arguments optionnels et ne sont pas spécifiés, et sont utilisés respectivement :
$ echo '{"json": "obj"}' | python -m json.tool { "json": "obj" } $ echo '{1.2:3.4}' | python -m json.tool Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Modifié dans la version 3.5: La sortie conserve maintenant l’ordre des données de l’entrée. Utilisez l’option pour sortir des dictionnaires triés alphabétiquement par clés.
Options de la ligne de commande
-
Le fichier JSON à valider ou réécrire élégamment :
$ python -m json.tool mp_films.json { "title": "And Now for Something Completely Different", "year": 1971 }, { "title": "Monty Python and the Holy Grail", "year": 1975 }
Si infile n’est pas spécifié, lit le document depuis .
-
Écrit la sortie générée par infile vers le fichier outfile donné. Autrement, écrit sur .
-
Trie alphabétiquement les dictionnaires par clés.
Nouveau dans la version 3.5.
-
Désactive l’échappement des caractères non ASCII, voir pour plus d’informations.
Nouveau dans la version 3.9.
-
Transforme chaque ligne d’entrée en un objet JSON individuel.
Nouveau dans la version 3.8.
-
Mutually exclusive options for whitespace control.
Nouveau dans la version 3.9.
-
Affiche le message d’aide.
Notes
-
Comme noté dans l’errata de la RFC 7159, JSON autorise les caractères littéraux U+2028 (LINE SEPARATOR) et U+2029 (PARAGRAPH SEPARATOR) dans les chaînes de caractères, alors que Javascript (selon le standard ECMAScript édition 5.1) ne le permet pas.