0

Really confused here. On my current project all data is stored in localStorage, and all objects/arrays have maximum one level of nesting. That results in many inconveniences and even duplicates of the same entities. I decided to refactor the data structure to something more convenient, but also more nested.

Would it be safe and reliable to JSON.stringify() / JSON.parse() back and forth such data? Some of google search results states that json does not support nested arrays, but it does support them! Or..?

what I have now:

    cars: [{id: car1, brand: 'bmw'}, {id: car2, brand: 'ford'}, {id: car3, brand: 'dodge'}]
    car1_details: {id: car1, color: 'blue', horsepower: 150, type: 'coupe'] 
    car2_details: {id: car2, color: 'red', horsepower: 180, type: 'hatchback'] 
    car3_details: {id: car3, color: 'yellow', horsepower: 200, type: 'sedan']

what I obviously want:

    cars: [ 
      {   
        id: car1,    
        brand: 'bmw',   
        details: {     
          color: 'blue',     
          horsepower: 150,     
          type: 'coupe' 
      }
    //etc 
    ]

I tested and it seems to be 100% working, but just wanted to make sure by asking you guys. Also there is this outdated post, but apparently it has been fixed since then.

Ishika Jain
  • 949
  • 2
  • 11
  • 23
  • That will be totally fine – bhspencer Mar 31 '22 at 13:36
  • 1
    The post you link to nobody could reproduce the issue and it was answered by the person who wrote it. I think they were just confused. – bhspencer Mar 31 '22 at 13:38
  • 1
    there is nothing unsafe about stringifying and parsing nested json. just make sure there aren't objects with functions attached to them and there aren't any recursive structures or anything like that. – bryan60 Mar 31 '22 at 14:42

0 Answers0