Skip to content
Advertisement

Remove white space in Json

How can I remove white space in the following code block and store in a javascript variable?

This is original format

{ "info":"First Json",
  "description":"Official Website",
  "timestamp":"1337936081",
   "rates":{
            "USD":"840", 
            "CHF":"1319", 
            "BDT":"298",
            } 
}    

This is the format I want

{"info":"Central Bank of Myanmar","description":"Official Website of Central Bank of Myanmar","timestamp":"1337936081","rates":{"USD":"840","CHF":"1319","BDT":"298","SGD":"632","JPY":"1053","GBP":"887","AUD":"759"}}     

Advertisement

Answer

var jsonWithWhitespaceRemoved = JSON.stringify(JSON.parse(variableWithJsonWhitespace))
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement