-
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
Merged
mongoKart
merged 8 commits into
mongodb:master
from
mongoKart:docsp-46694-extended-json
Feb 12, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2e91d99
autobuilder
mongoKart 0340a31
first draft
mongoKart 9e02193
add to toc
mongoKart adbb18c
fixes
mongoKart fb44651
Apply suggestions from code review
mongoKart f903bef
js feedback
mongoKart 3b0d4da
add format info
mongoKart 79a2ea7
fixes
mongoKart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,282 @@ | ||
.. _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. | ||
|
||
Extended JSON Formats | ||
--------------------- | ||
|
||
MongoDB Extended JSON features different string formats to represent BSON data. | ||
Each of the different formats conform to the JSON RFC | ||
and meet specific use cases. The **extended** format, also known as the | ||
**canonical** format, features specific representations for every BSON type | ||
for bidirectional conversion without loss of information. The **Relaxed mode** | ||
format is more concise and closer to ordinary JSON, but does not represent | ||
all the type information such as the specific byte size of number fields. | ||
|
||
See the following table to see a description of each format: | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
:widths: 10 40 | ||
|
||
* - Name | ||
- Description | ||
|
||
* - **Extended** | ||
- | Also known as the *canonical* format, this JSON representation avoids loss of | ||
BSON type information. | ||
| This format prioritizes type preservation at the loss of human-readability and | ||
interoperability with older formats. | ||
|
||
* - **Relaxed Mode** | ||
- | JSON representation that describes BSON documents with some type information loss. | ||
| This format prioritizes human-readability and interoperability at the loss of | ||
certain type information. | ||
|
||
* - **Shell** | ||
- | JSON representation that matches the syntax used in the MongoDB shell. | ||
| This format prioritizes compatibility with the MongoDB shell, which often uses | ||
JavaScript functions to represent types. | ||
|
||
* - **Strict** | ||
- | *Deprecated.* This representation is the legacy format that fully conforms to | ||
the `JSON RFC <http://www.json.org/>`__ which allows any JSON parser to read the type information. | ||
|
||
.. _extended_json_example_section: | ||
|
||
.. note:: | ||
|
||
The driver parses the ``$uuid`` Extended JSON type from a string to a | ||
``BsonBinary`` object of binary subtype 4. For more information about ``$uuid`` field | ||
parsing, see the | ||
:spec:`special rules for parsing $uuid fields </extended-json.rst#special-rules-for-parsing-uuid-fields>` | ||
section in the extended JSON specification. | ||
|
||
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. | ||
|
||
Extended JSON Examples | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The following examples show a document containing an ObjectId, date, and long | ||
number field represented in each Extended JSON format. Click the tab that | ||
corresponds to the format of the example you want to see: | ||
|
||
.. tabs:: | ||
|
||
.. tab:: Extended | ||
:tabid: extended-format | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"_id": { "$oid": "573a1391f29313caabcd9637" }, | ||
"createdAt": { "$date": { "$numberLong": "1601499609" }}, | ||
"numViews": { "$numberLong": "36520312" } | ||
} | ||
|
||
.. tab:: Relaxed Mode | ||
:tabid: relaxed-mode-format | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"_id": { "$oid": "573a1391f29313caabcd9637" }, | ||
"createdAt": { "$date": "2020-09-30T18:22:51.648Z" }, | ||
"numViews": 36520312 | ||
} | ||
|
||
.. tab:: Shell | ||
:tabid: shell-format | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"_id": ObjectId("573a1391f29313caabcd9637"), | ||
"createdAt": ISODate("2020-09-30T18:22:51.648Z"), | ||
"numViews": NumberLong("36520312") | ||
} | ||
|
||
.. tab:: Strict | ||
:tabid: strict-format | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"_id": { "$oid": "573a1391f29313caabcd9637" }, | ||
"createdAt": { "$date": 1601499609 }, | ||
"numViews": { "$numberLong": "36520312" } | ||
} | ||
|
||
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 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. | ||
The following example outputs an Extended JSON string in the Relaxed format: | ||
|
||
.. 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 | ||
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" | ||
}}} | ||
]''' | ||
|
||
Additional Information | ||
---------------------- | ||
|
||
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>`__ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The 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 😃