The EJSON.parse() method converts string values to JSON.
Syntax
The EJSON.parse() method takes a string as input and an optional modifier that controls the output format.
EJSON.parse(string, [options])
Command Fields
The EJSON.parse() method takes these fields:
Field | Type | Necessity | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
| string | Required | String | ||||||
| string | Optional | Modifies output types. The only option is
|
Behavior
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)"
Examples
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"), }, ] )
Format Input with EJSON.parse()
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 insalesCollectionas JSON pairs.db.salesRestored.insertMany()uses the JSON pairs to create thesalesRestoredcollection.
Use EJSON.parse() from the command line
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 usesfs.readFileSync()to read thesale.jsonfile as a string.EJSON.parse()formats the input string as JSON pairs.db.salesFromFile.insertMany()creates thesalesFromFilecollection 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.
Learn More
EJSON documentation
Mozilla Developer Network JSON.parse documentation