Show Output
<!DOCTYPE html> <html lang="en"> <head> <title>JavaScript Pretty Print JSON String</title> </head> <body> <script> // Sample JSON string var json = '{"name": "Peter", "age": 22, "country": "United States"}'; // Converting JSON string to object var obj = JSON.parse(json); // Pretty printing var str = JSON.stringify(obj, null, 4); // spacing level = 4 document.write("<pre>" + str + "</pre>"); </script> </body> </html>