Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meant by to prettify JSON

I have heard of sites that prettify/beautify JSON. What does it actually mean?

like image 922
Shamim Hafiz - MSFT Avatar asked Nov 30 '25 14:11

Shamim Hafiz - MSFT


2 Answers

This means a more human readable version of it. E.g. the following is valid json, but not well readable:

{"outcome" : "success", "result" : {"name" : "messaging-sockets", "default-interface" : "external", "include" : [], "socket-binding" : {"messaging" : {"name" : "messaging", "interface" : null, "port" : 5445, "fixed-port" : null, "multicast-address" : null, "multicast-port" : null}, "messaging-throughput" : {"name" : "messaging-throughput", "interface" : null, "port" : 5455, "fixed-port" : null, "multicast-address" : null, "multicast-port" : null}}}, "compensating-operation" : null}

After prettyfying this could look like this:

{
   "outcome":"success",
   "result":{
      "name":"messaging-sockets",
      "default-interface":"external",
      "include":[

      ],
      "socket-binding":{
         "messaging":{
            "name":"messaging",
            "interface":null,
            "port":5445,
            "fixed-port":null,
            "multicast-address":null,
            "multicast-port":null
         },
         "messaging-throughput":{
            "name":"messaging-throughput",
            "interface":null,
            "port":5455,
            "fixed-port":null,
            "multicast-address":null,
            "multicast-port":null
         }
      }
   },
   "compensating-operation":null
}
like image 176
Heiko Rupp Avatar answered Dec 03 '25 02:12

Heiko Rupp


It makes your code pretty, i.e. it indents it, makes sure stuff is aligned in a similar manner, all parenthesis are placed in a similar manner, etc.

Example

var obj = {apple: {red: 5, green: 1}, bananas: 9}; //JS object
var str = JSON.stringify(obj, null, 4); // spacing level 4, or instead of 4 you can write "\t" for tabulator

//The third argument from stringify function enables pretty printing and sets the spacing to use.
console.log(str); //now you can see well pretty printed JSON string in console

But if you want to do it on yourself then you can use the second parameter as a function. More about JSON.stringify function you can find here.

A lot of examples how to pretty print JSON string.

like image 36
Daniel Hilgarth Avatar answered Dec 03 '25 02:12

Daniel Hilgarth



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!