|
| 1 | +.. _pymongo-extended-json: |
| 2 | + |
| 3 | +============= |
| 4 | +Extended JSON |
| 5 | +============= |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: code examples, bson, relaxed, canonical, legacy |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +JSON is a data format that represents the values of objects, arrays, numbers, |
| 24 | +strings, booleans, and nulls. The **Extended JSON** format defines a reserved |
| 25 | +set of keys prefixed with "``$``" to represent field type information that |
| 26 | +directly corresponds to each type in BSON, the format that MongoDB uses to |
| 27 | +store data. |
| 28 | + |
| 29 | +Read Extended JSON |
| 30 | +------------------ |
| 31 | + |
| 32 | +You can read an Extended JSON string into a Python object by calling |
| 33 | +the ``bson.json_util.loads()`` method. This method parses an Extended |
| 34 | +JSON string and returns a Python list containing the data. |
| 35 | + |
| 36 | +The following example shows how you can read an example Extended JSON string into a |
| 37 | +list of dictionaries by using the ``loads()`` method: |
| 38 | + |
| 39 | +.. io-code-block:: |
| 40 | + |
| 41 | + .. input:: |
| 42 | + :language: python |
| 43 | + |
| 44 | + from bson.json_util import loads |
| 45 | + |
| 46 | + ejson_str = '''[ |
| 47 | + {"foo": [1, 2]}, |
| 48 | + {"bar": {"hello": "world"}}, |
| 49 | + {"code": { |
| 50 | + "$scope": {}, |
| 51 | + "$code": "function x() { return 1; }" |
| 52 | + }}, |
| 53 | + {"bin": { |
| 54 | + "$type": "80", |
| 55 | + "$binary": "AQIDBA==" |
| 56 | + }} |
| 57 | + ]''' |
| 58 | + |
| 59 | + doc = loads(ejson_str) |
| 60 | + print(doc) |
| 61 | + |
| 62 | + .. output:: |
| 63 | + :language: none |
| 64 | + :visible: false |
| 65 | + |
| 66 | + data = [ |
| 67 | + {'foo': [1, 2]}, |
| 68 | + {'bar': {'hello': 'world'}}, |
| 69 | + {'code': Code('function x() { return 1; }', {})}, |
| 70 | + {'bin': Binary(b'\x01\x02\x03\x04', 128)} |
| 71 | + ] |
| 72 | + |
| 73 | +Write Extended JSON |
| 74 | +------------------- |
| 75 | + |
| 76 | +You can write an Extended JSON string from a list of dictionaries |
| 77 | +by calling the ``bson.json_util.dumps()`` method. |
| 78 | +In this example, we output the Extended JSON in the Relaxed mode format. |
| 79 | + |
| 80 | +.. io-code-block:: |
| 81 | + |
| 82 | + .. input:: |
| 83 | + :language: python |
| 84 | + |
| 85 | + from bson import Code, Binary |
| 86 | + from bson.json_util import dumps |
| 87 | + |
| 88 | + doc = [ |
| 89 | + {'foo': [1, 2]}, |
| 90 | + {'bar': {'hello': 'world'}}, |
| 91 | + {'code': Code('function x() { return 1; }', {})}, |
| 92 | + {'bin': Binary(b'\x01\x02\x03\x04', 128)} |
| 93 | + ] |
| 94 | + |
| 95 | + ejson_str = dumps(doc) |
| 96 | + print(ejson_str) |
| 97 | + |
| 98 | + .. output:: |
| 99 | + :language: none |
| 100 | + :visible: false |
| 101 | + |
| 102 | + '''[ |
| 103 | + {"foo": [1, 2]}, |
| 104 | + {"bar": {"hello": "world"}}, |
| 105 | + {"code": { |
| 106 | + "$code": "function x() { return 1; }", |
| 107 | + "$scope": {} |
| 108 | + }}, |
| 109 | + {"bin": { |
| 110 | + "$binary": { |
| 111 | + "base64": "AQIDBA==", |
| 112 | + "subType": "80" |
| 113 | + }}} |
| 114 | + ]''' |
| 115 | + |
| 116 | +By default, the ``dumps()`` method returns the Extended JSON string in the Relaxed |
| 117 | +format. To specify a different format, pass one of the following values for the |
| 118 | +``json_options`` parameter: |
| 119 | + |
| 120 | +- ``CANONICAL_JSON_OPTIONS``: Returns the Extended JSON string in Canonical format. |
| 121 | +- ``LEGACY_JSON_OPTIONS``: Returns the Extended JSON string in Legacy format. |
| 122 | + We recommend using Relaxed or Canonical format instead. |
| 123 | + |
| 124 | +The following example shows how to output Extended JSON in the Canonical format: |
| 125 | + |
| 126 | +.. io-code-block:: |
| 127 | + |
| 128 | + .. input:: |
| 129 | + :language: python |
| 130 | + |
| 131 | + from bson import Code, Binary |
| 132 | + from bson.json_util import dumps, CANONICAL_JSON_OPTIONS |
| 133 | + |
| 134 | + doc = [ |
| 135 | + {'foo': [1, 2]}, |
| 136 | + {'bar': {'hello': 'world'}}, |
| 137 | + {'code': Code('function x() { return 1; }', {})}, |
| 138 | + {'bin': Binary(b'\x01\x02\x03\x04', 128)} |
| 139 | + ] |
| 140 | + |
| 141 | + ejson_str = dumps(doc, json_options=CANONICAL_JSON_OPTIONS) |
| 142 | + print(ejson_str) |
| 143 | + |
| 144 | + .. output:: |
| 145 | + :language: none |
| 146 | + :visible: false |
| 147 | + |
| 148 | + '''[ |
| 149 | + {"foo": [ |
| 150 | + {"$numberInt": "1"}, |
| 151 | + {"$numberInt": "2"} |
| 152 | + ]}, |
| 153 | + {"bar": {"hello": "world"}}, |
| 154 | + {"code": { |
| 155 | + "$code": "function x() { return 1; }", |
| 156 | + "$scope": {} |
| 157 | + }}, |
| 158 | + {"bin": { |
| 159 | + "$binary": { |
| 160 | + "base64": "AQIDBA==", |
| 161 | + "subType": "80" |
| 162 | + }}} |
| 163 | + ]''' |
| 164 | + |
| 165 | +More Information |
| 166 | +---------------- |
| 167 | + |
| 168 | +To learn more about JSON, BSON, and Extended JSON, see |
| 169 | +`our article about JSON and BSON <https://www.mongodb.com/resources/basics/json-and-bson>`__ |
| 170 | +and :manual:`</reference/mongodb-extended-json/>` in the {+mdb-server+} manual. |
| 171 | + |
| 172 | +For more information about the methods and types in ``bson.json_util``, see the following |
| 173 | +API documentation: |
| 174 | + |
| 175 | +- `loads() <{+api-root+}bson/json_util.html#bson.json_util.loads>`__ |
| 176 | +- `dumps() <{+api-root+}bson/json_util.html#bson.json_util.dumps>`__ |
| 177 | +- `CANONICAL_JSON_OPTIONS <{+api-root+}bson/json_util.html#bson.json_util.CANONICAL_JSON_OPTIONS>`__ |
| 178 | +- `LEGACY_JSON_OPTIONS <{+api-root+}bson/json_util.html#bson.json_util.LEGACY_JSON_OPTIONS>`__ |
0 commit comments