Are whitespace characters insignificant in JSON?
Are blank characters like spaces, tabs and carriage returns ignored in json strings?
For example, is {"a":"b"}
equal to {"a" : "b"}
?
Yes, blanks outside a double-quoted string literal are ignored in the syntax. Specifically, the ws
production in the 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 )
In standard JSON, whitespace outside of string literals is ignored, as has been said.
However, since your question is tagged C#, I should note that there's at least one other case in C#/.NET where whitespace in JSON does matter.
The DataContractJsonSerializer
uses a special __type
property to support deserializing to the correct subclass. This property is required to be the first property in an object, and to have no whitespace between the property name and the preceeding {
. See this previous thread:
DataContractJsonSerializer doesn't work with formatted JSON?
At least, I have tested that the no-whitespace requirement is true as of .NET 4. Perhaps this will be changed in a future version to bring it more into line with the JSON standard?