For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

EJSON.parse()

The EJSON.parse() method converts string values to JSON.

The EJSON.parse() method takes a string as input and an optional modifier that controls the output format.

EJSON.parse(string, [options])

The EJSON.parse() method takes these fields:

Field
Type
Necessity
Description

value

string

Required

String EJSON.parse() transforms into JSON key-value pairs

options

string

Optional

Modifies output types. The only option is { relaxed: <boolean> }.

  • true: Returns native JavaScript types when possible

  • false: Prefer to return BSON types

You can call EJSON.parse() from inside an interactive mongosh session or from the system command line using --eval.

Call EJSON.parse() from an interactive session:

EJSON.parse(string)

Call EJSON.parse() from the system command line:

mongosh --eval "EJSON.parse(string)"

To try these examples, first create the sales collection:

db.sales.insertMany( [
{ custId: 345, purchaseDate: ISODate("2023-07-04"), quantity: 4, cost: Decimal128("100.60"), },
{ custId: 346, purchaseDate: ISODate("2023-07-12"), quantity: 3, cost: Decimal128("175.45"), },
{ custId: 486, purchaseDate: ISODate("2023-08-01"), quantity: 9, cost: Decimal128("200.53"), },
] )

EJSON.parse() accepts a string as input. For this example, use the EJSON.stringify() method to export the sales collection as a string.

let salesCollection = EJSON.stringify( db.sales.find().toArray() )

Use EJSON.parse() to format the string for methods like db.collection.insertMany() that expect JSON pairs:

db.salesRestored.insertMany( EJSON.parse( salesCollection ) )
  • EJSON.parse() formats the values in salesCollection as JSON pairs.

  • db.salesRestored.insertMany() uses the JSON pairs to create the salesRestored collection.

To import string data from an external source such as a file or an API call, use EJSON.parse() with the mongosh --eval method.

For this example, save the sales collection as a file.

let salesCollection = EJSON.stringify( db.sales.find().toArray() )
fs.writeFileSync( 'sales.json', salesCollection )

The code creates a file on your local system called sales.json. To import the file and create a new collection, exit mongosh and run an --eval operation from the command line.

# Note: This example is formatted to fit on the page.
mongosh --quiet \
--eval "db.salesFromFile.insertMany( \
EJSON.parse( fs.readFileSync( 'sales.json', 'utf8' ) ) )"
  • EJSON.parse() takes a string as input. This example uses fs.readFileSync() to read the sale.json file as a string.

  • EJSON.parse() formats the input string as JSON pairs.

  • db.salesFromFile.insertMany() creates the salesFromFile collection from the JSON pairs.

Warning

Validate Untrusted Input Before Converting JSON to BSON

If you convert a JSON string to BSON and use the resulting BSON in a query, update, or command, an attacker can inject operators or unexpected field values that change the meaning of the operation. This risk is greatest when the JSON originates from a user, an API request, or another untrusted source.

To learn more about validating input before conversion, see Validate Untrusted Input Before Converting JSON to BSON.