Menu

Post image 1
Post image 2
1 / 2
0

7 Things Most Developers Don't Know About JSON.parse() and JSON.stringify()

DEV Community·Snappy Tools·25 days ago
#PNnGhGYP
Reading 0:00
15s threshold

JSON.parse() and JSON.stringify() look simple. You call one to get data in, call the other to get it out. But both have second and third arguments that most developers never touch — and those arguments unlock features that would otherwise require custom code. Here are seven things worth knowing. 1. JSON.stringify() has a space argument Everyone knows the first argument. Most people have no idea about the third: const data = { name : " Alice " , age : 30 , tags : [ " admin " , " editor " ] }; // Default — minified JSON . stringify ( data ); // '{"name":"Alice","age":30,"tags":["admin","editor"]}' // Indented with 2 spaces JSON . stringify ( data , null , 2 ); // { // "name": "Alice", // "age": 30, // "tags": [ // "admin", // "editor" // ] // } // Indented with a tab character JSON . stringify ( data , null , ' \t ' ); Enter fullscreen mode Exit fullscreen mode The space argument accepts either a number (number of spaces) or a string (used as the indent).…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More