JSON is a simple standard for the structured encoding and representation of data. What began in the 1990s with XML (eXtensible Markup Language), JSON now continues with a lean data format for describing variables, objects, and arrays. The aim is a simple, human-readable representation of data. Application developers very often use JavaScript object descriptions in combination with Ajax. Most programming languages therefore have parsers that translate a JSON declaration into a corresponding representation, for example in the form of variables or objects.
Because it is closely related to XML and YAML, JSON fulfills similar tasks. It stores a wide variety of data in a structured and machine-readable way. There is also a close connection to the JavaScript programming language. JSON is not limited to it, however, and is not one hundred percent compatible with it either. The simple data format was derived from JavaScript and has since been used primarily to serialize program-internal data structures for data storage and communication through interfaces.
Structure and Data Types in JSON
JSON has two different, universal data structures. The advantage here is the broad support from a large number of programming languages, including Java, C, C++, Perl, and Python. The structures are divided into the following types.
Name/Value or Key/Value Pairs
{
"name":"Mustermann"
}Ordered List of Values
[
{
"vorname" : "Michael",
"name":"Mustermann",
"mail" : "Michael.Mustermann@domain.de"
},
{
"vorname" : "Elisa",
"name":"Bridal",
"mail" : "Elisa.Bridal@domain.de"
}
]Special syntax rules apply to both type declarations, however, and they differ from JavaScript syntax in some respects. The two are therefore not directly compatible.
- In a name/value pair, the identifiers or field names and the values must each be enclosed in quotation marks. The two are separated from each other by a colon.
- Objects in JSON are defined by curly braces. An object can contain an unlimited amount of data in the form of name/value pairs.
- Arrays represent an ordered list of values. An array consists of an opening square bracket, the values, and a closing square bracket. Multiple values are separated from one another by commas.
This simple syntax is simplified further by a manageable number of data types. JSON has only a few data types, but they are no less important for that.
- String. A string can consist of Unicode characters and is enclosed in quotation marks. It can also represent escape sequences with special functions.
- Number. An integer or floating-point number is declared in a similar way to Java or C, with the exception of hexadecimal or octal formats.
- Object. An object represents a collection of comma-separated name/value pairs and is grouped with curly braces.
- Array. An array lists comma-separated values. An array is enclosed in square brackets.
- Boolean. A Boolean value can take the states "true" or "false."
- Null. The data type null represents missing values or values that are not yet known in attributes, in order to avoid undefined states.
The following example shows a valid JSON declaration for a sample exchange rate query on a trading venue.
{
"realtime_currency_exchange_rate": {
"from_currency_code": "BTC",
"to_currency_code": "CNY",
"exchange_rate": "43774.50023880",
"last_refreshed": "2018-09-06 11:25:44",
"time_zone": "UTC"
}
}A Deep Dive into JSON
In application development, it is very important that the data represented in JSON is validated before it is used. Validation usually begins by applying a data schema to the data structure to be checked. There are different approaches to this. For one, developers can check their schemas and test data in advance with suitable validator tools. Here, the chosen schema is applied to a list of data, and the tool checks whether the data fully conforms to the schema.
Despite the proximity to XML and YAML, you cannot assume that all JSON data formats are identical and compatible with one another. Since 2017, there have been two different but synchronized standards for JSON that cover different aspects of the data format. The ECMA-404 standard defines the JSON syntax, and RFC 8259 aims at increased interoperability. Although JSON descends from JavaScript, there is a partial incompatibility, which is most noticeable in the data types.
A fixed standard has not yet been reached. The current drafts merely represent proposals that are valid for only six months and then become "non-binding." After that, a new proposal is published that reflects all innovations and current adjustments. This process repeats until a sufficiently practicable common ground has been reached among all parties. Only then does the perpetual draft status turn into a binding version.
On the one hand, JSON simplifies the data format for communication. On the other hand, it is too lenient in its handling of line and paragraph separator characters. In addition, JSON does not support JavaScript data types such as Date, Error, Function, Set, or undefined, which can lead to problems during data exchange. In many cases, the values therefore have to be converted. JSON also shows weaknesses with numeric values. The reliable definition of integers, decimal numbers, or numbers in scientific notation causes problems. Corresponding routines in the common programming languages resolve these problems in a targeted way and represent the data correctly.
Conclusion
As a consistent further development of XML, and modeled on JavaScript and YAML, JSON clearly shows that even complex tasks such as data serialization and object declarations can be kept very lean and simple. A major advantage is also the human-readable form of the data used, which makes working with even complex data structures considerably easier. The simple structures of JSON also deliver very good performance, which is an advantage above all when using APIs and when network bandwidth is constantly under heavy load.