Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: how do I remove all the white spaces from a JSON string except the ones in the values?

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.

like image 955
j3d Avatar asked Sep 04 '14 22:09

j3d


People also ask

Does JSON Stringify remove white spaces?

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.

How do you remove spaces from a JSON file?

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.

How do you remove all the spaces from a string in JS?

JavaScript String trim() The trim() method removes whitespace from both sides of a string. The trim() method does not change the original string.

What is JSON trim?

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.).


2 Answers

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.

like image 53
Niels Keurentjes Avatar answered Sep 20 '22 13:09

Niels Keurentjes


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\" }"));
like image 36
Barmar Avatar answered Sep 19 '22 13:09

Barmar