What is JSON?

JSON stands for Java Script Object Notation…

With internet and the general networking of computer systems, the need for data exchange has always increased. Platform independence is very important for systems on the web. With JSON (Java Script Object Notation) a very lightweight, human- and machine-readable data format has been established. You can find everything important for practical use here.

Content

The JSON Document

The data packet in JSON format is called a JSON document. The document has a consistent, auditable structure. It is always exchanged between applications as a whole. For human processing of a JSON document, it is easiest to have the document as a file in the file system. Here, the file extension “*.JSON” is normally used. The content of a JSON document is always object-oriented.

Correlation of JSON and JavaScript

The definition of JSON comes from a subset of the JavaScript language definition of the ECMA-262 standard. The fact that JSON has this origin and the name “Java Script Object Notation” does not mean that JSON is limited to use in JavaScript. JSON is a completely independent data format with no binding to the JavaScript language. There are many built-in functions for working with JSON in JavaScript, which makes it especially easy to use JSON in JavaScript. However, many other languages also have corresponding frameworks. But in case of doubt, the simple text format of JSON can be read and processed in any programming language.

Fields of application and practical use

With its very simple structure and encoding in the Unicode character set, JSON is the ideal format for exchanging data between systems. And this without having to take into account operating systems, computer architectures or programming languages.
Data is stored in JSON format and transmitted as a data packet to the target system via any path (http, file, TCP/IP, database, etc.). Due to the structure and the integrated descriptive elements, the target system can very easily evaluate the data again. Since the data is exchanged as a closed packet/document, JSON is very well suited for asynchronous processes. JSON is most commonly used in Web environments, but is establishing itself more and more in various application areas.

Basic structure

The formatting of the content in a JSON document must follow a strict structure. The original specification was written by Douglas Crockford and is documented as a JSON definition. A JSON document always begins and ends with ‘{‘ and ‘}’. The contents are placed between the curly brackets. The curly brackets enclose an object. Further objects can be defined in the document itself. A data field is introduced by a name and the value follows a colon. The value can then be an object again.

Structure


{
"Data field1": "Value"
"Data object."
{
"Field 1": 123
"Field2": "Text"
"Field3Array":["Value1", "Value2", "Value3", "Value4"] }
}
}

Display of numbers and character strings

The variables stored in JSON can be stored in the following types:

Boolean: The truth value can be either true or false. The keywords are used without quotation marks (e.g. “active”: true).

Number: A number is represented with the digits 0-9, optionally with a dot and sign. An exponent can also be used (e.g. “distance”: 3.1e+12).

Strings: A string is any text placed between quotation marks. A character string is also called a string (e.g. “City”: “Hamburg”).

zero: If a variable is empty, it can be expressed by assigning null. This should not be mixed up with the number 0 or an empty string. Zero means truly “nothing” (example “link”: zero).

Example

A rather more complex example explains the production order with several components. The production order is described by some basic fields and an array containing the components. The components are described as separate objects with their respective description fields.

{
"Order number": 4711,
"Quantity": 15000.0,
"Item": "Product A",
"Planned date": "6/4/2020",
"Release": true,
"Components":[
{
"Name": "Component A",
"Material number": 292345,
"Quantity": 345,123,
"Storage areas": [ 3, 6, 23 ] }, {
"Name": "Component B",
"Material number": 908431,
"Quantity": 12034,123,
"Storage areas": [ 1, 2, 9 ] },
{
"Name": "Component C",
"Material number": 928398,
"Quantity": 2623.2,
"Storage locations": [ 10 ] },
] }

Comparison JSON and XML

XML (Extensible Markup Language) is a description language. In its core, the description of data is similar to JSON. In XML, the description with tags results in a higher overhead. Data can be described not only by tags, but also by attributes (e.g.: <TAG1 AttributeA=”Value”>TEXT</TAG1>). Therefore, during data exchange, sender and receiver must have very clear information about the agreed structure.
In contrast, the structure in JSON is very flexible and in any case completely understandable for the receiver and can be converted into objects without any prior structure agreement.
XML is therefore suitable for clearly defined data interfaces and JSON for flexible data exchange.

JSON in NoSQL

A well-known use case for JSON are the document-based databases according to the NoSQL concept. These databases accept JSON documents for storage and return them in queries.
Due to the flexible structure of JSON any JSON documents can be transferred into a document-oriented NoSQL database. The query functions of these databases can access all fields and objects in the documents without prior schema definition and thus return the documents with matching criteria in queries. A schema definition as with classical SQL is completely unnecessary.
An example for such a database is the MongoDB database. The OPC Router has a plug-in to transfer data to MongoDB databases.

JSON and MQTT

The MQTT protocol is a simple and lightweight transport protocol for data exchange. It is often used by devices and services that have to deal with limited bandwidth and computing capacity. The data format of MQTT is not specified. But the data payload is very often formatted as a JSON document, because it is ideal for this application. For this reason many MQTT connection partners can be found who publish their data in the form of JSON.

JSON in industry

In industrial environments, the JSON format is growing in popularity. This is mainly driven by the ever closer integration of machines and field level devices with web technologies. Through field devices and software systems with REST interfaces (What is REST?) and integration of sensors and devices via MQTT, JSON is already very widespread. Currently, the use of NoSQL is being added and the growing trend towards Machine Learning and Artificial Intelligence make JSON indispensable as a simple but flexible data format.

Devices / Services

Services providing information via REST interface often return their data as JSON. An example is the weather service OpenWeatherMap. This example call returns the current weather in London as JSON: https://openweathermap.org/
A detailed example can be found in our tutorial article on how to connect to OpenWeatherMap.

Devices that use JSON are often data sources that send their data via MQTT and are therefore part of the IoT. PFC controllers from Wago for example can send JSON documents via MQTT.

Run a free test now!

Get your personal link to the most recent OPC Router version and sign up for product news.

Test now!