Given the following json...
var body = "{ \"name\": \"test\", \"description\": \"test json\", \"website\": \"domain.com\" }"
... how do I remove all the white spaces except the ones in the values?
I've tried the following regexp...
var body = "{ \"name\": \"test\", \"description\": \"test json\", \"website\": \"domain.com\" }".replace(/\r?\n|\r/g, "").replace(/\s+/g, "")
... but it also removes the spaces in the values (i.e. description
):
{"name":"test","description":"testjson","website":"domain.com"}
I need to obtain
{"name":"test","description":"test json","website":"domain.com"}
Tx.
Calling JSON. stringify(data) returns a JSON string of the given data. This JSON string doesn't include any spaces or line breaks by default.
First remove the leading and trailing spaces from the key using trim() function. Remove all the spaces, newline or tab present in the key using replace() function and add underscore "_" between the word of the key instead of whitespace.
JavaScript String trim() The trim() method removes whitespace from both sides of a string. The trim() method does not change the original string.
Trim is also a javascript inbuild function that removes space from both ends of the string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).
var body = ' { "name": "test", "description": "test json", "website": "domain.com" } ';
JSON.stringify(JSON.parse(body))
Returns:
{"name":"test","description":"test json","website":"domain.com"}
Which is exactly what you want. And yes, JSON.stringify
with 1 parameter is guaranteed to give the shortest possible JSON, meaning no obsolete whitespace outside keys and values. That's the default because JSON is, in nearly all situations, intended to be a highly efficient recursive data serialization method - unnecessary whitespace has no use there by default.
The full syntax is actually:
JSON.stringify(value[, replacer [, space]])
The optional third parameter defaults to false
- if set to another value it produces 'pretty print' with extra whitespace or lead-in strings:
The space argument may be used to control spacing in the final string. If it is a number, successive levels in the stringification will each be indented by this many space characters (up to 10). If it is a string, successive levels will indented by this string (or the first ten characters of it).
As a final note, you shouldn't even want to use a regexp for this. Regexps are for making sense out of character patterns, not for processing structured data like XML, HTML or JSON. In those cases, use an XML, DOM or JSON parser respectively and process the results in there.
Parse the JSON, then stringify it again. Parsing it will convert the JSON to a Javascript object, and then stringifying it will return the shortest JSON representation:
var body = JSON.stringify(JSON.parse("{ \"name\": \"test\", \"description\": \"test json\", \"website\": \"domain.com\" }"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With