Javascript - JSON.stringify()
Code Life
By Pierre Marsaa
Turning an Object Into Text
When your game saves a player's progress to a file or sends a Codémon to a server over the internet, it cannot send a live JavaScript object. Files and network messages can only carry plain text (strings).
If you try to glue an object into a string directly, JavaScript does not write out its properties — it just turns it into useless placeholder text:
↗️
Loading editor...
↶
↷
To turn an object into real, usable text, JavaScript gives you JSON.stringify(). It takes any object or array and returns a string formatted in JSON — a universal text format understood by almost every programming language and server:
↗️
Loading editor...
↶
↷
Notice the two differences between the original object and the JSON string:
1.
Every property key is wrapped in double quotes ("name", "level"), with extra spaces removed.
2.
The entire result has type "string", ready to be saved to a file or sent across the network.
Verify