JSON
The JSON (JavaScript Object Notation) encoding is used by the JSON-LD encoding.
Lax Syntax
JSON is published in a wide range of contexts and comes from many sources, including humans. In some cases, we use a "lax" parsing mode which is more forgiving of common syntax errors. Although such errors will still generate an error message recommending a fix, we will still attempt to continue parsing and validating the document as it was likely intended. When enabled, the following, non-standard behaviors are used.
- A block comment (
/* ... */
) will be ignored. - A line comment (
// ...
) will be ignored. - An extra comma (
,
) within an object or array will be ignored. - An extra semicolon (
;
) after a root value will be ignored. - A string using a literal new line character will be converted to its escape sequence (
\n
). - A string using an invalid UTF-8 sequence will be converted to the Unicode replacement character (
U+FFFD
).
Common Errors
Below are some of the most common errors that can cause the parsing of JSON documents to fail. Although some of these may be recoverable, they should always be fixed at the source to ensure data interoperability.
Literal New Line Character
Within a string, a new line character may only be used in its escaped form (\n
) or as a Unicode sequence (\u000a
). A literal new line is not valid JSON syntax.
With Lax Syntax, these new lines are included in the final string.
Examples
Usage | Code |
Invalid | "hello world" |
Valid | "hello\nworld" |
Valid | "hello\u000aworld" |
Comment
A block or line comment is not valid JSON syntax.
With Lax Syntax, these comments are ignored.
Examples
Usage | Code |
Invalid | "hello" // world |
Invalid | "hello" /* world */ |
Valid | "hello" |
Extra Comma
Within an array or object, a comma may only be used between members. A repeated comma or a comma after the last member is not valid JSON syntax.
With Lax Syntax, these commas are ignored.
Examples
Usage | Code |
Invalid | ["hello","world",] |
Invalid | {"hello":"world",} |
Invalid | ["hello",,"world"] |
Invalid | {"hello":"world",,"world":"hello"} |
Valid | ["hello","world"] |
Valid | {"hello":"world"} |
Extra Semicolon
A semicolon is not valid JSON syntax and, unlike JavaScript, may not be used at the end of JSON data.
With Lax Syntax, these semicolons are ignored.
Examples
Usage | Code |
Invalid | "hello world"; |
Valid | "hello world" |
Invalid Unicode Sequence
Within a string, extended UTF-8 or UTF-16 characters may be encoded using UTF-8 escape sequences or surrogate pairs. An warning will be logged, but, unlike other errors which may halt parsing, these invalid sequences will automatically be replaced by the Unicode replacement character (U+FFFD
).
Examples
Usage | Code |
Invalid | "\ud834" |
Valid | "\ud834\udd1e" |
Invalid | "\uvwxy" |
Technical Notes
- Reference: RFC 8259
- Media Type:
application/json
- File Name:
*.json