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!

10
  • 2
    I can't reproduce it. [33, 0] is most certainly there and not null: jsfiddle.net/cw80ou5n Commented Mar 3, 2023 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]. Commented Mar 3, 2023 at 20:23
  • 1
    Please post a minimum 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! Commented Mar 3, 2023 at 20:24
  • 2
    It sounds like something is replacing brackets[id] asynchronously. Commented Mar 3, 2023 at 20:31
  • 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! Commented Mar 4, 2023 at 1:20

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.