Appendix D. Using JSON formatting
JSON is a simple and widely used way to represent data exchanged between applications, especially in REST-based communication. Because modern apps often send and receive information through REST endpoints, understanding JSON is a basic and essential skill for working with HTTP requests and responses.
At its core, JSON represents object instances through attribute-value pairs. Objects are written with curly braces, attributes are named with double quotes, string values are also quoted, numbers are left unquoted, and each name is separated from its value with a colon. Multiple attributes are separated by commas, and the JSON object itself does not carry an explicit name or type, only the data it contains.
JSON also supports nesting and collections. An attribute can hold another object as its value, allowing structures to be built recursively as deeply as needed. When representing multiple objects together, JSON uses brackets to create an array, with each object entry separated by commas. This makes JSON flexible enough to describe both individual records and groups of related data in a compact, readable form.
Figure D.1 When you implement business logic, it sometimes implies establishing communication between multiple apps. Most often, you use JSON to format the data the apps exchange. To implement and test your REST endpoints, you need to understand JSON.
Figure D.2 Describing an object instance in JSON. We surround the attribute-value pairs with curly braces. A colon separates the attribute name and its value. The attribute-value pairs are separated with commas.
FAQ
What is JSON and why is it important in REST communication?
JSON, or JavaScript Object Notation, is a common format for exchanging data between apps in HTTP requests and responses. It is especially important because REST endpoints frequently use JSON to communicate.How does JSON represent an object instance?
JSON represents an object instance by listing its attribute-value pairs inside curly braces{ }.What symbols are used to define an object in JSON?
Curly braces are used to define an object in JSON. The attribute-value pairs are written between the braces.How are attribute names written in JSON?
Attribute names in JSON are written between double quotes.How are string values written in JSON?
String values are written between double quotes. If a string contains a double quote character, that quote must be escaped with a backslash\.How are numeric values written in JSON?
Numeric values are written without quotes.How are attribute names and values separated in JSON?
An attribute name and its value are separated by a colon:.Can a JSON object contain another object?
Yes. A JSON object can contain another object as the value of one of its attributes, allowing nested objects.How do you represent a collection of objects in JSON?
A collection of objects is represented with brackets[ ], and each object in the collection is separated by commas.
Spring Start Here, Second Edition ebook for free