Joey Yi Zhao
Joey Yi Zhao

Reputation: 42500

Does JSON.parse support parsing string without double quote

I have below json data. The problem is on the last field "totalChildMillis" : NumberLong(2). The NumberLong(2) is not quoted. When I run JSON.parse I got an exception about this field. Is there a way for JSON.parse to ignore double quote? I don't want to pre-parse the string by myself so I am looking for a way to handle this automatically for me.

{
  "executionStages" : {
    "stage" : "SINGLE_SHARD",
    "nReturned" : 10000,
    "executionTimeMillis" : 3,
    "totalKeysExamined" : 0,
    "totalDocsExamined" : 10000,
    "totalChildMillis" : NumberLong(2)
  }
}

Upvotes: 2

Views: 435

Answers (1)

Guig
Guig

Reputation: 10197

This is not a valid json, so JSON.parse will rightfully fail. It seems you can make it a valid json by doing

var jsonString = rawString.replace(/NumberLong\((\d*)\)/g, "$1")

and then

JSON.parse(jsonString)

If NumberLong is coming from Mongo, you probably can get it to output a valid json directly

Upvotes: 1

Related Questions