Is double quotes on property names required for bson.EJSON.parse?


I recently tried the tool bson (npm i bson) and read this document:
and noticed that in the example some of the keys are not double quoted, like “submittedAt” and “answer”. But when I copy and paste the code and test locally, I got an error says: unexpected token on the position of the property name “answer”. So what is the problem?

Node.js version: v10.15.0
“bson”: “^4.4.0”

Hi Zhixin, welcome to the forums! :wave:

This is an issue where our docs code examples are incorrect. Thank you for pointing this out!

The JSON spec requires double-quoted property names, so you’re correct that those keys should also have quotes. I’m about to push an update to the docs to fix this which should be live within the next 20-30 mins.

The code blocks for JSON.parse() and EJSON.parse() should resemble this:

const jsonString = `{
  "answer": 42,
  "submittedAt": "2020-03-02T16:50:24.475Z"
const jsonObject = JSON.parse(jsonString);

const ejsonString = `{
  "answer": {
    "$numberLong": "42"
  "submittedAt": {
    "$date": {
      "$numberLong": "1583167607977"
const ejsonObject = EJSON.parse(ejsonString);

Sorry for the confusion and thanks again for pointing out the issue!


Hi Nick,

Thank you very much for answering. You resolved my confusion.
I might still have a follow up question. The reason I looked into the JSON parse API is that I am recently working on a JSON editor project, which basically beautify and edits JSON string, parse it to JSON objects and then save to MongoDB. As users always have to add double quotes to property names, to make it more convenient, I am looking for parsers that allow unquoted keys.
I remembered that when I am using mongo shell to insert documents to db, I can use unquoted keys, like db.insert({ name: “smith”, age: 21}). If my understanding is right, when reading from standard input, we are actually reading it as Strings, so there must be some step in which String is parsed to Object. So I am really curious about how Mongo Shell does that?
Thank you again for your time.

1 Like

Sounds like a cool project!

Like I mentioned, the JSON standard spec requires double-quoted property names, so any JSON-spec compliant parser like EJSON.parse() will require them too.

If you see unquoted field names that’s almost definitely JavaScript code (.js) and not JSON (.json). The mongodb shell is actually a full-blown JavaScript REPL so that’s why you see unquoted field names there.

If you want to parse regular JS object you’d need to either:

  • write a string parser yourself to add the quotes
  • find a library that does this. Maybe a JSON superset like hjson which doesn’t require quoted fields? (I’ve never used the hjson library before, just an example)

Hi Nick,

Thank you very much for your answer. Your suggestion gives me a new idea to dig in. Hope I can find a solution. Thanks.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.