How does JSON.parse manage 'undefined'?
javascript, json
Solution
`undefined` is not a valid JSON token. When converting an undefined value to JSON, the correct practice is to render it as null.
Problem
I transformed this: ``` function MangaElt(obj) { "use strict"; this.mirror = obj.mirror; this.name = obj.name; this.url = obj.url; if (obj.lastChapterReadURL !== undefined) { this.lastChapterReadURL = obj.lastChapterReadURL; this.lastChapterReadName = obj.lastChapterReadName; } else { this.lastChapterReadURL = null; this.lastChapterReadName = null; } this.listChaps = []; if (obj.listChaps !== undefined && obj.listChaps !== null && obj.listChaps !== "null") { if (!isArray(obj.listChaps)) { this.listChaps = JSON.parse(obj.listChaps); } } this.read = 0; if (obj.read !== undefined && obj.read !== null && obj.read !== "null") { this.read = obj.read; } } ``` Into this: ``` function MangaElt(obj) { "use strict"; this.mirror = obj.mirror; this.name = obj.name; this.url = obj.url; this.lastChapterReadURL = obj.lastChapterReadURL || null; this.lastChapterReadName = obj.lastChapterReadName || null; this.listChaps = JSON.parse(obj.listChaps) || []; this.read = obj.read || 0; this.update = obj.update || 1; } ``` As you can see, the code is now more readable and compact. The snippet works under normal circumstances just fine. The thing is that I don't have sometimes all the values in the `obj` object, so, I expect some `undefined`'s here and there. And that is the reason of my questions: - Why `JSON.parse` interpret a `undefined` as string, trowing as say the MDN, "syntax error" for `undefined`? - Should then, I, check before the values get parsed if the value is a proper string? - Shouldn't JSON.parse, check whenever the value parsed is `undefined` and just return `undefined`? (This may rise arguments, so, if you believe that is good as is, just ignore this question or state that I'm just wrong with my train of trough) - If #2 is affirmative, then just adding some conditional as the first snipped should be enough, right? Or should I go to the function that calls MangaElt and make sure that `obj.listChaps` is an array and forget about `JSON.parse` here?. (This is always an array or a pseudo-array in a string, and since this is a collaborative project, someone may have a reason for this) For the curious that may ask, 'what's the error you are getting?' is this: ``` Error in event handler for 'undefined': Unexpected token u SyntaxError: Unexpected token u at Object.parse (native) at new MangaElt (chrome-extension://nhjloagockgobfpopemejpgjjechcpfd/js/MangaElt.js:44:25) at readManga (chrome-extension://nhjloagockgobfpopemejpgjjechcpfd/js/background.js:410:24) at chrome-extension://nhjloagockgobfpopemejpgjjechcpfd/js/background.js:607:9 at Event.dispatchToListener (event_bindings:356:21) at Event.dispatch_ (event_bindings:342:27) at Event.dispatch (event_bindings:362:17) at miscellaneous_bindings:165:24 at Event.dispatchToListener (event_bindings:356:21) at Event.dispatch_ (event_bindings:342:27) event_bindings:346 ``` This is what already existing entries looks like, which do not generate errors. This scenario is what motivated my question. The type of keys are always the same and are tested beforehand: - `name` is a string - `mirror` is a string - `url` is a string - `listChaps` is an "array" inside a string - `ts` and `upts` are integers BTW, `obj` is an object, but I think that it's almost impossible to miss. Also, this is a Chrome extension, but I don't think that's relevant. Complete script here.