2

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)
  }
}
Saugat
  • 1,309
  • 14
  • 22
Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
  • No, it is invalid json. If you want to pass a method with a parameter, you can make them two separate fields and then do something like window[data.totalChildMethod].call(data.totalChildMillis); – nixkuroi Apr 14 '17 at 00:01
  • Who produced above JSON? You or third party? – engineforce Apr 14 '17 at 00:58
  • Mongodb can produce outputs like `NumberLong(2)` – Guig Apr 14 '17 at 01:37
  • @engineforce The mongo shell produced the JSON. I send an explain command to mongo shell and got that output from standard stream. – Joey Yi Zhao Apr 14 '17 at 01:42
  • [Force mongodb to output strict JSON](http://stackoverflow.com/questions/32097209/force-mongodb-to-output-strict-json) may solve your problem. – engineforce Apr 14 '17 at 09:46

1 Answers1

1

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

Guig
  • 9,891
  • 7
  • 64
  • 126