-
Notifications
You must be signed in to change notification settings - Fork 20
DOCSP-46694 - Extended JSON #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
2e91d99
0340a31
9e02193
adbb18c
fb44651
f903bef
3b0d4da
79a2ea7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
.. _pymongo-extended-json: | ||
|
||
============= | ||
Extended JSON | ||
============= | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: code examples, bson, relaxed, canonical, legacy | ||
|
||
Overview | ||
-------- | ||
|
||
JSON is a data format that represents the values of objects, arrays, numbers, | ||
strings, booleans, and nulls. The **Extended JSON** format defines a reserved | ||
set of keys prefixed with "``$``" to represent field type information that | ||
directly corresponds to each type in BSON, the format that MongoDB uses to | ||
store data. | ||
|
||
Read Extended JSON | ||
------------------ | ||
|
||
You can read an Extended JSON string into a Python object by calling | ||
the ``bson.json_util.loads()`` method. This method parses an Extended | ||
JSON string and returns a Python list containing the data. | ||
|
||
The following example shows how you can read an example Extended JSON string into a | ||
list of dictionaries by using the ``loads()`` method: | ||
|
||
.. io-code-block:: | ||
|
||
.. input:: | ||
:language: python | ||
|
||
from bson.json_util import loads | ||
|
||
ejson_str = '''[ | ||
{"foo": [1, 2]}, | ||
{"bar": {"hello": "world"}}, | ||
{"code": { | ||
"$scope": {}, | ||
"$code": "function x() { return 1; }" | ||
}}, | ||
{"bin": { | ||
"$type": "80", | ||
"$binary": "AQIDBA==" | ||
}} | ||
]''' | ||
|
||
doc = loads(ejson_str) | ||
print(doc) | ||
|
||
.. output:: | ||
:language: none | ||
:visible: false | ||
|
||
[ | ||
{'foo': [1, 2]}, | ||
{'bar': {'hello': 'world'}}, | ||
{'code': Code('function x() { return 1; }', {})}, | ||
{'bin': Binary(b'\x01\x02\x03\x04', 128)} | ||
] | ||
|
||
Write Extended JSON | ||
------------------- | ||
|
||
You can write an Extended JSON string from a list of dictionaries | ||
by calling the ``bson.json_util.dumps()`` method. | ||
In this example, we output the Extended JSON in the Relaxed mode format. | ||
mongoKart marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it expected that a user will know what "Relaxed format" is when reading this, or should we provide a brief description? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our other docs have more info on the formats (see the Java page I linked earlier), but I thought it was redundant to the Server manual, so I just went with a link. One option is to the move the link higher in the page and make it more visible? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. Yeah I think making the link more visible and closer to the first mention would be helpful. Maybe linking it inline on first mention (although I know we don't do that often) or just adding a "To learn more about..." somewhere closer
mongoKart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. io-code-block:: | ||
|
||
.. input:: | ||
:language: python | ||
|
||
from bson import Code, Binary | ||
from bson.json_util import dumps | ||
|
||
doc = [ | ||
{'foo': [1, 2]}, | ||
{'bar': {'hello': 'world'}}, | ||
{'code': Code('function x() { return 1; }', {})}, | ||
{'bin': Binary(b'\x01\x02\x03\x04', 128)} | ||
] | ||
|
||
ejson_str = dumps(doc) | ||
print(ejson_str) | ||
|
||
.. output:: | ||
:language: none | ||
:visible: false | ||
|
||
'''[ | ||
{"foo": [1, 2]}, | ||
{"bar": {"hello": "world"}}, | ||
{"code": { | ||
"$code": "function x() { return 1; }", | ||
"$scope": {} | ||
}}, | ||
{"bin": { | ||
"$binary": { | ||
"base64": "AQIDBA==", | ||
"subType": "80" | ||
}}} | ||
]''' | ||
|
||
By default, the ``dumps()`` method returns the Extended JSON string in the Relaxed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the Relaxed format? Would users be reasonably expected to know what that means without prior exposure? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to keep this page focused on just the PyMongo aspects of it to avoid redundancy with the server docs (linked on this page). That said, both you and the docs reviewer asked whether this page should include background info on the format, which makes me think we should 😃 |
||
format. To specify a different format, pass one of the following values for the | ||
``json_options`` parameter: | ||
|
||
- ``CANONICAL_JSON_OPTIONS``: Returns the Extended JSON string in Canonical format. | ||
- ``LEGACY_JSON_OPTIONS``: Returns the Extended JSON string in Legacy format. | ||
We recommend using Relaxed or Canonical format instead. | ||
|
||
The following example shows how to output Extended JSON in the Canonical format: | ||
|
||
.. io-code-block:: | ||
|
||
.. input:: | ||
:language: python | ||
|
||
from bson import Code, Binary | ||
from bson.json_util import dumps, CANONICAL_JSON_OPTIONS | ||
|
||
doc = [ | ||
{'foo': [1, 2]}, | ||
{'bar': {'hello': 'world'}}, | ||
{'code': Code('function x() { return 1; }', {})}, | ||
{'bin': Binary(b'\x01\x02\x03\x04', 128)} | ||
] | ||
|
||
ejson_str = dumps(doc, json_options=CANONICAL_JSON_OPTIONS) | ||
print(ejson_str) | ||
|
||
.. output:: | ||
:language: none | ||
:visible: false | ||
|
||
'''[ | ||
{"foo": [ | ||
{"$numberInt": "1"}, | ||
{"$numberInt": "2"} | ||
]}, | ||
{"bar": {"hello": "world"}}, | ||
{"code": { | ||
"$code": "function x() { return 1; }", | ||
"$scope": {} | ||
}}, | ||
{"bin": { | ||
"$binary": { | ||
"base64": "AQIDBA==", | ||
"subType": "80" | ||
}}} | ||
]''' | ||
|
||
More Information | ||
---------------- | ||
mongoKart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
To learn more about JSON, BSON, and Extended JSON, see | ||
`our article about JSON and BSON <https://www.mongodb.com/resources/basics/json-and-bson>`__ | ||
and :manual:`Extended JSON </reference/mongodb-extended-json/>` in the {+mdb-server+} manual. | ||
|
||
For more information about the methods and types in ``bson.json_util``, see the following | ||
API documentation: | ||
|
||
- `loads() <{+api-root+}bson/json_util.html#bson.json_util.loads>`__ | ||
- `dumps() <{+api-root+}bson/json_util.html#bson.json_util.dumps>`__ | ||
- `CANONICAL_JSON_OPTIONS <{+api-root+}bson/json_util.html#bson.json_util.CANONICAL_JSON_OPTIONS>`__ | ||
- `LEGACY_JSON_OPTIONS <{+api-root+}bson/json_util.html#bson.json_util.LEGACY_JSON_OPTIONS>`__ |
Uh oh!
There was an error while loading. Please reload this page.