The labs on this site need JavaScript to run. The same content is written out below.
Fundamentals › Networking
What does JSON look like, and what mistakes make it unreadable?
Why it exists — If programs send data in text shapes that each decided on its own, the receiving side must write separate code to read that shape for each partner. A format that uses an opening tag and a closing tag for each value, such as XML, tends to make the text long. JSON is written with only a few rules, curly braces, square brackets, double quotes, and commas, so it is easy for people to read, and almost every language reads and writes it by the same rules.
Where it's used today — In Spring, a Java object returned by @RestController is converted to JSON text by Spring Boot with Jackson and sent, and JSON text that comes in the request body is turned into an object when it is received by a parameter marked with @RequestBody. JSON is also usually used to exchange data with external services such as maps, weather, and payment. For configuration files that need comments, YAML (application.yml), which allows comments, is used instead.
When programs exchange data (for example, when a server sends member information to a browser), they cannot send an object in memory as it is. So they write it out as text by an agreed set of rules, and the receiving side reads that text and builds an object in its own language again. JSON is the most widely used set of rules. What you see in the code box is the JSON text itself. Inside { }, write "name": value pairs joined by commas. Wrap names and string values in double quotes, and write numbers without quotes. [ ] is an array of several values. Whether there are quotes decides the kind of the value, so 30 is a number and "30" is a string. In this lab you change the quotes around the name, the value of age, and the commas one at a time, and feed the same text to two parsers (programs that read text and build an object): JSON.parse in Node.js and Jackson in Java. You see whether the text can be read and what kind of value the age that was read is. JSON has no syntax for comments, so the code box shows the text only, with no explanations.
Checked against real execution · Fed the characters of all 18 combinations as they are to JSON.parse in Node.js 24.18.0 and to Jackson 3.1.5 (default settings) and checked them
{
"name": "Alice",
"age": 30,
"tags": ["vip", "new"]
}
We used two parsers. One is JSON.parse in Node.js 24.18.0 (the standard JavaScript parser). The other is Jackson 3.1.5 (jackson-databind and jackson-core) run on Java 21.0.12.1 with the default settings, that is, readTree of the parser made with JsonMapper.builder().build(). For all eighteen combinations, we fed the same characters as the code box (lines end with \n, with no line break at the end) to both parsers as they are, and checked against the model: success or failure, the line, column, and character at the position of the error, the kind of age that was read (typeof for JSON.parse, the node kind for Jackson), and the names and values that were read. The two parsers made the same success or failure decision in all eighteen combinations, and when it failed, the error position was the same too. When it failed, both parsers only raised an error and did not return the part they had read, and when the text had more than one error, they reported only the first one they met. The original error messages differ between the two (for example, JSON.parse says "Expected property name or '}' in JSON at position 4" and Jackson says "was expecting double-quote to start property name"), and the screen shows only the kind of error, written in English. The original messages are in the Probe output. The "age + 1 on the object that was read (JavaScript)" on the screen is JavaScript behavior. It is the value of adding 1 to age of the object read with JSON.parse (we also checked typeof: the number 30 gives the number 31, and the string "30" gives the string "301"), and it is not Java behavior. When the same text is read with Jackson, age is a string node, and the intValue() of that node raised an error (JsonNodeException), while asInt() and reading into a record with an integer field both gave 30. That is Jackson's conversion behavior, so it is not used on the screen. Some parsers have options to accept text that breaks the rules, depending on the settings. What we checked is only this: turning on three Jackson JsonReadFeature options (ALLOW_SINGLE_QUOTES, ALLOW_UNQUOTED_PROPERTY_NAMES, ALLOW_TRAILING_COMMA) makes a name wrapped in single quotes, a name without quotes, and a comma after the last value readable (text with a missing comma was still not readable). All the checks on the screen were made with the default settings. We did not look at other parsers or at the settings of JSON.parse. Text with a comment in it was also not readable by either parser with the default settings. What we checked against the documentation is RFC 8259 section 1 (JSON is a text format for the serialization of structured data), section 2 (grammar; whitespace around the structural characters has no meaning), section 3 (a value is an object, array, number, or string, or false, null, or true, and these words are lowercase), section 4 (grammar of objects: members joined by commas, and a name is a string), section 5 (grammar of arrays), section 6 (numbers are written as decimal digits), section 7 (a string begins and ends with quotation marks, and the only quotation mark is the double quote, %x22), and section 9 (a parser must accept all texts that conform to the grammar, and may accept forms other than JSON). The documents were opened only as summaries, so we could not check them against the full original text. The statement that a trailing comma and single quotes are not allowed is not written directly in the document. We based it on the fact that the grammar in sections 4 and 5 has no shape with nothing after a comma and no single-quote shape, together with the run results above. The JSON on the screen covers only the object, array, string, and number among the kinds of values, and does not cover true, false, null, nested objects, or escapes inside strings (such as \n). The in-depth lab on the same subject (how a server converts between objects and JSON) is "REST APIs and message converters".
Struck-through text was removed from the first version of the code; highlighted text was added.