|
| 1 | +.. _mongosh-ejson-parse: |
| 2 | + |
| 3 | +============= |
| 4 | +EJSON.parse() |
| 5 | +============= |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. include:: /includes/links-urls/ejson.rst |
| 14 | + |
| 15 | +The ``EJSON.parse()`` method converts string values to JSON. |
| 16 | + |
| 17 | +Syntax |
| 18 | +------ |
| 19 | + |
| 20 | +The ``EJSON.parse()`` method takes a string as input and an optional |
| 21 | +modifier that controls the output format. |
| 22 | + |
| 23 | +.. code-block:: javascript |
| 24 | + :copyable: false |
| 25 | + |
| 26 | + EJSON.parse(string, [options]) |
| 27 | + |
| 28 | +Command Fields |
| 29 | +-------------- |
| 30 | + |
| 31 | +The ``EJSON.parse()`` method takes these fields: |
| 32 | + |
| 33 | +.. list-table:: |
| 34 | + :header-rows: 1 |
| 35 | + |
| 36 | + * - Field |
| 37 | + - Type |
| 38 | + - Necessity |
| 39 | + - Description |
| 40 | + |
| 41 | + * - ``value`` |
| 42 | + - string |
| 43 | + - Required |
| 44 | + - String ``EJSON.parse()`` transforms into JSON key-value pairs |
| 45 | + |
| 46 | + * - ``options`` |
| 47 | + - string |
| 48 | + - Optional |
| 49 | + - Modifies output :ref:`types <mongo-shell-data-type>`. The only |
| 50 | + option is ``{ relaxed: <boolean> }``. |
| 51 | + |
| 52 | + .. list-table:: |
| 53 | + :header-rows: 1 |
| 54 | + :widths: 30 70 |
| 55 | + |
| 56 | + * - Value |
| 57 | + - Effect |
| 58 | + |
| 59 | + * - ``true`` |
| 60 | + - Returns native JavaScript types when possible |
| 61 | + |
| 62 | + * - ``false`` |
| 63 | + - Prefer to return BSON types |
| 64 | + |
| 65 | +Behavior |
| 66 | +-------- |
| 67 | + |
| 68 | +You can call ``EJSON.parse()`` from inside an interactive ``mongosh`` |
| 69 | +session or from the system command line using ``--eval``. |
| 70 | + |
| 71 | +Call ``EJSON.parse()`` from an interactive session: |
| 72 | + |
| 73 | +.. code-block:: javascript |
| 74 | + :copyable: false |
| 75 | + |
| 76 | + EJSON.parse(string) |
| 77 | + |
| 78 | +Call ``EJSON.parse()`` from the system command line: |
| 79 | + |
| 80 | +.. code-block:: shell |
| 81 | + :copyable: false |
| 82 | + |
| 83 | + mongosh --eval "EJSON.parse(string)" |
| 84 | + |
| 85 | +Examples |
| 86 | +-------- |
| 87 | + |
| 88 | +To try these examples, first create the ``sales`` collection: |
| 89 | + |
| 90 | +.. code-block:: javascript |
| 91 | + |
| 92 | + db.sales.insertMany( [ |
| 93 | + { custId: 345, purchaseDate: ISODate("2023-07-04"), quantity: 4, cost: Decimal128("100.60"), }, |
| 94 | + { custId: 346, purchaseDate: ISODate("2023-07-12"), quantity: 3, cost: Decimal128("175.45"), }, |
| 95 | + { custId: 486, purchaseDate: ISODate("2023-08-01"), quantity: 9, cost: Decimal128("200.53"), }, |
| 96 | + ] ) |
| 97 | + |
| 98 | +Format Input with EJSON.parse() |
| 99 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 100 | + |
| 101 | +``EJSON.parse()`` accepts a string as input. For this example, use the |
| 102 | +:ref:`EJSON.stringify() <mongosh-ejson-stringify>` method to export the |
| 103 | +``sales`` collection as a string. |
| 104 | + |
| 105 | +.. code-block:: javascript |
| 106 | + |
| 107 | + let salesCollection = EJSON.stringify( db.sales.find().toArray() ) |
| 108 | + |
| 109 | +Use ``EJSON.parse()`` to format the string for methods like |
| 110 | +:method:`db.collection.insertMany()` that expect JSON pairs: |
| 111 | + |
| 112 | +.. code-block:: javascript |
| 113 | + |
| 114 | + db.salesRestored.insertMany( EJSON.parse( salesCollection ) ) |
| 115 | + |
| 116 | +- ``EJSON.parse()`` formats the values in ``salesCollection`` as JSON |
| 117 | + pairs. |
| 118 | + |
| 119 | +- :method:`db.salesRestored.insertMany() <db.collection.insertMany()>` |
| 120 | + uses the JSON pairs to create the ``salesRestored`` collection. |
| 121 | + |
| 122 | +Use EJSON.parse() from the command line |
| 123 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 124 | + |
| 125 | +To import string data from an external source such as a file or an API |
| 126 | +call, use ``EJSON.parse()`` with the ``mongosh --eval`` method. |
| 127 | + |
| 128 | +For this example, save the ``sales`` collection as a file. |
| 129 | + |
| 130 | +.. code-block:: javascript |
| 131 | + |
| 132 | + let salesCollection = EJSON.stringify( db.sales.find().toArray() ) |
| 133 | + |
| 134 | + fs.writeFileSync( 'sales.json', salesCollection ) |
| 135 | + |
| 136 | +The code creates a file on your local system called ``sales.json``. To |
| 137 | +import the file and create a new collection, exit ``mongosh`` and run an |
| 138 | +``--eval`` operation from the command line. |
| 139 | + |
| 140 | +.. code-block:: javascript |
| 141 | + |
| 142 | + # Note: This example is formatted to fit on the page. |
| 143 | + |
| 144 | + mongosh --quiet \ |
| 145 | + --eval "db.salesFromFile.insertMany( \ |
| 146 | + EJSON.parse( fs.readFileSync( 'sales.json', 'utf8' ) ) )" |
| 147 | + |
| 148 | +- ``EJSON.parse()`` takes a string as input. This example uses |
| 149 | + ``fs.readFileSync()`` to read the ``sale.json`` file as a string. |
| 150 | + |
| 151 | +- ``EJSON.parse()`` formats the input string as JSON pairs. |
| 152 | + |
| 153 | +- ``db.salesFromFile.insertMany()`` creates the ``salesFromFile`` |
| 154 | + collection from the JSON pairs. |
| 155 | + |
| 156 | +Learn More |
| 157 | +---------- |
| 158 | + |
| 159 | +- |ejsonUrl| documentation |
| 160 | +- Mozilla Developer Network |mdn-json-parse| documentation |
| 161 | + |
0 commit comments