Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object to string, and vice-versa

Say I had the object:

var object = {
    1: [2,5,"hi"],
    hi: {hihi: 1}
};

How would I convert that to a string, and then back, preserving all the information? I would need this to work for a big object, with the values themselves being objects.

This is not a duplicate, the others didn't involve getting the object back.

like image 594
Fuzzyzilla Avatar asked Feb 01 '26 01:02

Fuzzyzilla


1 Answers

Below is a live demo of how you can convert an object to a string and back using JSON.stringify() and JSON.parse().

Open your browser console and you can see that all attributes are preserved after the conversion to a string and back.

var object = {
    1: [2,5,"hi"],
    hi: {hihi: 1}
};

console.log(object);

var strobj = JSON.stringify(object);

console.log(strobj);

var unstrobj = JSON.parse(strobj);

console.log(unstrobj);
like image 60
Maximillian Laumeister Avatar answered Feb 02 '26 18:02

Maximillian Laumeister



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!