Are whitespace inconsequential in JSON?

Will blank characters like spaces, tabs and carriage returns be ignored in json strings?

For example, {"a":"b"} is equal to {"a" : "b"} ?

+44
json
Nov 11 '10 at 1:11
source share
2 answers

Yes, spaces outside a double-quoted string literal are ignored in the syntax. In particular, creating ws in a JSON grammar in RFC 4627 shows:

 Insignificant whitespace is allowed before or after any of the six structural characters. ws = *( %x20 / ; Space %x09 / ; Horizontal tab %x0A / ; Line feed or New line %x0D ; Carriage return ) 
+54
Nov 11 '10 at 1:12
source share

In standard JSON, spaces outside of string literals are ignored, as was said.

However, since your question is marked with C #, I should note that there is at least one other case in C # /. NET where space in JSON matters.

The DataContractJsonSerializer uses the special __type property to support deserialization into the correct subclass. This property must be the first property in the object and not have spaces between the name of the property and the preceding { . See this topic: DataContractJsonSerializer not working with formatted JSON?

At least I tested that the no-whitespace requirement is true as .NET 4. Perhaps this will be changed in a future version to bring it into compliance with the JSON standard?

+3
May 19 '13 at 14:16
source share



All Articles