1

Parsing a string JSON object nullifies numbers that are in nested arrays.

I'm parsing a string JSON object in javascript.

The string: (throw it into https://jsonformatter.org/json-pretty-print if you want, but its pretty simple)

{"teams":[["Roman Bravo-Young (PSU)","vito Arujau (COR)"],["Spencer Lee (IOWA)","Pat GLory (PRIN)"],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null]],"results":[[[[33,0],[1,0],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null]],[[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null]],[[null,null],[null,null],[null,null],[null,null]],[[null,null],[null,null]],[[null,null],[null,null]]]]}

The code:

console.log(brackets[id].data);
var adminData = JSON.parse(brackets[id].data);
console.log("ADMIN DATA");
console.log(adminData);

The console output:

enter image description here

The JSON.parse is turning the [33, 0] and [1, 0] arrays into null values. I have never seen this before in my decades of programming.

Even stranger: Doing a second JSON.parse(brackets[id].data); call will cause the FIRST call in the console to show the correct data, but the second log in the console will still show everything as null.

How can this happen? Thank you!

Barmar
  • 741,623
  • 53
  • 500
  • 612
Splen
  • 21
  • 5
  • 2
    I can't reproduce it. `[33, 0]` is most certainly there and not null: https://jsfiddle.net/cw80ou5n/ – Unmitigated Mar 03 '23 at 20:19
  • 1
    I agree with @Unmitigated -- not seeing a problem here and cannot reproduce. Entering your JSON into DevTools as a string, then converting to an object using `JSON.parse(string)`, I can access `obj.results[0][0][0]` which results in `[33,0]`. – Josh Mar 03 '23 at 20:23
  • I can't reproduce it either https://playcode.io/1259420 – Pipe Mar 03 '23 at 20:23
  • 1
    Please post a [minimum reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) that show the actual data in the code and reproduces the result you're claiming. I suspect merely attempting to do this will show you the route of the actual problem (which isn't in the code you've shown nhere) - it often does! – Robin Zigmond Mar 03 '23 at 20:24
  • 2
    It sounds like something is replacing `brackets[id]` asynchronously. – Barmar Mar 03 '23 at 20:31
  • It looks like you're using the Google Chrome developer console. A weird thing about this console is that it doesn't evaluate object contents until you click the expand arrows. So if you print out an object, then that object gets modified, then you expand the old printout, the expansion will show that object's CURRENT values instead of the values that were present at the time of the original printout. If you want to avoid this, try printing out the `JSON.stringify`-d version of your object to the console rather than doing the print-and-expand-by-clicking thing. – nullromo Mar 03 '23 at 22:14
  • @nullromo interesting, definitely going to try that! – Splen Mar 04 '23 at 00:54
  • @Barmar that was my first suspicious, but I'm only making a single call when the page loads to populate the "brackets" object, then never touching it again. It is read-only from that point on. I also could not repro in fiddles of my own. I'll see if I can somehow make something reproducable in a sterile environment, though I will state that this is a very minimal webpage. – Splen Mar 04 '23 at 00:54
  • 1
    @nullromo you were 100% correct. I was passing this array as a reference into a new variable, which was being modified, but because it was a reference, it affected the original variable as well. Chrome was showing the object modified AFTER the log. Brilliant. Thank you! – Splen Mar 04 '23 at 01:20
  • 1
    See https://stackoverflow.com/questions/34277332/javascript-console-log-reporting-object-properties-incorrectly regarding the console showing modified objects. – Barmar Mar 04 '23 at 17:50

1 Answers1

1

I was passing this array as a reference into a new variable, which was being modified, but because it was a reference, it affected the original variable as well. Chrome was showing the object modified AFTER the log.

Screenshot with the problem variable highlighted, which modifies the reference later

Splen
  • 21
  • 5