Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #38632

JSON, definitions, standards - things I do not understand

From retired developer <retired.developer@protonmail.com>
Newsgroups comp.lang.javascript
Subject JSON, definitions, standards - things I do not understand
Date 2020-08-04 03:33 +0200
Message-ID <hors7qF9m1cU1@mid.individual.net> (permalink)

Show all headers | View raw


Hello,

I hope this is the right place for my questions, if not, please redirect 
me to the proper group.
Thanks.

Beside JavaScript, I use another programing language too where JSON is 
supported.

During a data import (data in JSON format) I found a typo in my imported 
data, but the import was without any error message.
What the heck is that, I thought, and started some digging...

Here are the results, and I'm stumped.

The html file (see below) was opened with Google Chrome.
The output (see below too) looks a little bit buggy, but I think, the 
staff at Google will know, what they do, so there must be some kind of 
explanation for this output.

Is there someone here who can enlighten me?

Thank you,
Julius

--- the html-file --------------------------
<!DOCTYPE html>
<html>
<script>
function jsonThings() {
   var arr1, arr2, str, tmp;
   var i, j;

   str = "[ 11,, null]" ; // the ,, was a typo
   try { arr1 = JSON.parse(str); add(str,"Must be a coincidence..."); }
   catch(e) { add(str,e.message); }

   arr2 = [11,,null];
   add(JSON.stringify(arr2),"empty field (,,) is parsed!");

   size = arr2.length;
   add("array-size: "+size, "nonexistent (undefined) items are counted");

   tmp = "";
   j = 0;
   for (i=0; i<size; i++) { tmp += i + " " + typeof(arr2[i]) + "<br>"; j++;}
   add(tmp, "FOR loop is executed "+j+" times");

   tmp = "";
   j=0;
   for (i in arr2) { tmp += i + " " + typeof(arr2[i]) + "<br>"; j++; }
   add(tmp,"The loop is executed "+j+" times!");

   tmp = arr2[0];
   for (i=1; i<size; i++) { tmp += ","+arr2[i]; }
   add(JSON.stringify(arr2),"["+tmp+"] Stringify vs. content. Which one 
is correct?");

}

function add(str,msg) {
   var obj = document.getElementById("msg");
   var itm = document.createElement("P");
   itm.innerHTML = str + " --> " + msg;
   obj.appendChild(itm)
}
</script>

<body onload="jsonThings();">
<div id="msg" />
</body>
</html>
--- end-of-html-file --------------------------------------

--- the-output --------------------------
[ 11,, null] --> Unexpected token , in JSON at position 5

[11,null,null] --> empty field (,,) is parsed!

array-size: 3 --> nonexistent (undefined) items are counted

0 number
1 undefined
2 object
--> FOR loop is executed 3 times

0 number
2 object
--> The loop is executed 2 times!

[11,null,null] --> [11,undefined,null] Stringify vs. content. Which one 
is correct?
--- end-of-output -------------------------------------


-- 
An old Windows has old security holes.
A new Windows has new security holes.
Another OS has other security holes.
For safety you must care yourself.

Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

JSON, definitions, standards - things I do not understand retired developer <retired.developer@protonmail.com> - 2020-08-04 03:33 +0200
  Re: JSON, definitions, standards - things I do not understand Julio Di Egidio <julio@diegidio.name> - 2020-08-04 02:07 -0700
    Re: JSON, definitions, standards - things I do not understand retired developer <retired.developer@protonmail.com> - 2020-08-04 12:54 +0200
      Re: JSON, definitions, standards - things I do not understand Julio Di Egidio <julio@diegidio.name> - 2020-08-04 06:00 -0700
      Re: JSON, definitions, standards - things I do not understand Julio Di Egidio <julio@diegidio.name> - 2020-08-04 06:25 -0700
      Re: JSON, definitions, standards - things I do not understand Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-08-05 10:15 +0200
        Re: JSON, definitions, standards - things I do not understand Julio Di Egidio <julio@diegidio.name> - 2020-08-05 02:30 -0700

csiph-web