Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does JSON.parse manage 'undefined'?

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:

  1. Why JSON.parse interpret a undefined as string, trowing as say the MDN, "syntax error" for undefined?
  2. Should then, I, check before the values get parsed if the value is a proper string?
  3. 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)
  4. 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.

like image 468
Braiam Avatar asked Jul 15 '13 06:07

Braiam


People also ask

How does JSON parse work?

How Does JSON Parse Work? The JSON parse function takes data that's in a text format and then converts it into JavaScript. Usually, these take the form of JavaScript objects, but the parse function can be used directly on arrays as well.

Does JSON Stringify remove undefined?

JSON. stringify will omit all object attributes that are undefined .

How do you check if a JSON object is undefined?

You want to be checking for undefined , not null . If you want to cover for all falsy values (null, undefined, zero...) you may just turn the first line into : if (this. ItemAttributes.

Why do we need to parse JSON?

JSON as a string json or profiles. json above, that file actually contains text in the form of a JSON object or array, which happens to look like JavaScript. Just like with text files, if you want to use JSON in your project, you'll need to parse or change it into something your programming language can understand.


1 Answers

undefined is not a valid JSON token. When converting an undefined value to JSON, the correct practice is to render it as null.

like image 78
matt3141 Avatar answered Sep 19 '22 06:09

matt3141