How to minify and pretty print JSON in JavaScript

7 years ago Lalit Bhagtani 0

How to minify and pretty print JSON in JavaScript :- 

In this tutorial, we will learn how to minify and pretty print JSON in JavaScript. Java Script provide us a method JSON.stringify(), which can convert Java Script value or object to JSON string.
How to minify and pretty print JSON in JavaScript

Method :-    JSON.stringify ( value[, replacer[, space]]) 

This method takes three parameters as an argument :-

  1. value :- It is a value or an Object, which you like to convert to JSON string.
  2. replacer :- It is a function that can change the behaviour of the stringify method or can be an array of String and Number that can filter the properties of the object to be included in the JSON string. If nothing is provided, all properties of the object will be included in the resulting JSON string.
  3. space :- It is a String or a Number, which is used to insert white spaces into the output JSON string. If it is a Number, then it indicates the number of space characters to be use as a white space. Maximum number which can be use is 10, any value greater than this will be adjusted to 10 where as any value less than 1 will indicate no space. If it is a String, it is used as a white space. If length of string is greater than 10, then only first 10 characters will be used as a white space. If nothing is provided then no white space will be used.

Sample :-

JSON for book type :-

{
   "id": "01",
   "language": "Java",
   "edition": "third",
   "author": [ "Herbert", "Json", "Andrew" ],
   "chapters": {
                 "Chapter-1": "Introduction",
                 "Chapter-2": "OOP's Concept"
               }
} 

Minify :-

To minify json string, first we will convert it to an Object by using JSON.parse() method, then we will use JSON.stringify() method to minify it by passing space ( o ) as an argument.

<script>

var json = '{  "id": "01",  "language": "Java",  "edition": "third",  "author": ["Herbert", "Json", "Andrew"],  "chapters": {"Chapter-1": "Introduction","Chapter-2": "OOP\'s Concept"}}'; 
var jsonObject = JSON.parse(json);

var newString = JSON.stringify(jsonObject, null, 0)

alert(newString);

</script>

Pretty Print :-

To pretty print json string, first we will convert it to an Object by using JSON.parse() method and then we will use JSON.stringify() method by passing space ( \t ) as an argument.

<script>

var json = '{"id": "01","language": "Java","edition": "third","author": ["Herbert", "Json", "Andrew"],"chapters": {"Chapter-1": "Introduction","Chapter-2": "OOP\'s Concept"}}'; 
var jsonObject = JSON.parse(json);

var newString = JSON.stringify(jsonObject, null, '\t')

alert(newString);

</script>

You may also like :- 

  1. How to parse JSON in JavaScript

References :- 

  1. JSON.stringify() Docs

That’s all for how to minify and pretty print JSON in JavaScript, If you liked it, please share your thoughts in comments section and share it with others too.