JSON in relational databases
For some years now, most relational databases have offered functions for the direct storage and processing of JSON data. PostgreSQLone of the leading database platforms, introduced the data type json to support JSON data in a relational environment and thus build a bridge between structured and unstructured data.
Advantages of JSON integration
The combination of relational databases and JSON makes it possible to store both structured and semi-structured or unstructured data in one system. This increases flexibility and simplifies the storage of complex data structures. This is particularly advantageous for data with varying attributes, as is often the case in the IoT sector. IoT devices send data with different structures and attributes that can change depending on the device or platform. JSON enables the flexible storage of such heterogeneous data without rigid table schemas. One example of this are devices such as thermostats or sensors, whose attributes can be available as strings, integer values, Booleans or arrays - with JSON, these can be stored directly and flexibly.
The integration of JSON often eliminates the need for complex join operations, which can significantly improve performance when working with nested or extensive data structures.
Challenges and disadvantages of using JSON
However, the use of JSON in relational databases also poses some challenges. One of these is the loss of relational data integrity, as JSON data is not subject to the same integrity rules as classic relational data. This can lead to inconsistencies. In addition, the complexity of queries can increase, especially when using ORM (Object-Relational Mapping) frameworks. In addition, JSON usage can increase storage requirements in some scenarios, as redundant information can occur multiple times within JSON data.
JSON vs. JSONB: A comparison in PostgreSQL
In PostgreSQL, version 9.4 introduced the data type jsonb introduced to address some of the performance challenges of the original json-data type. The decisive difference: While json stores the data as plain text and it has to be analyzed again for each query, stores jsonb the data in binary format. JSONB data is therefore already analyzed and structured, which improves query speed. JSONB data can also be indexed, which further increases the performance of complex queries.
When to use JSON and when to use JSONB?
In most applications jsonb as it is optimized for regularly queried and indexed JSON data. The json-data type is more suitable if JSON data is only stored but rarely or never queried. For less complex JSON structures, the use of json also save some storage space.
Practical examples of the use of JSONB
Example 1: Simple storage of IoT device information
{
"device_id": "thermostat-001",
"device_type": "smart_thermostat",
"location": {
"room": "living_room",
"floor": 1,
"building": "home"
}
}
Queries in PostgreSQL:
With JSONB, specific data can be queried easily and efficiently. Here are two example queries in PostgreSQL:
SELECT * FROM devices WHERE device_info->>'device_type' = 'smart_thermostat'; SELECT * FROM devices WHERE device_info->'location'->>'room' = 'living_room';
Queries with JPA CriteriaBuilder:
JSONB access can also be realized in Java with the CriteriaBuilder:
Predicate deviceTypePredicate = cb.equal(
cb.function("jsonb_extract_path_text", String.class, root.get("deviceInfo"), cb.literal("device_type")),
deviceType
);
Predicate roomPredicate = cb.equal(
cb.function("jsonb_extract_path_text", String.class, root.get("deviceInfo"), cb.literal("location"), cb.literal("room")),
room
);
query.where(cb.and(deviceTypePredicate, roomPredicate));
Example 2: Storage of key-value data in JSON format
Another example of the storage of more complex key value data:
[
{"key": "name", "value": "test-name"},
{"key": "isOnline", "value": true},
{"key": "languages", "value": ["english", "german"]}
]
Queries in PostgreSQL:
SELECT jsonb_element->>'value' FROM jsonb_array_elements(jsonb_column) AS jsonb_element WHERE jsonb_element ->> 'key' = 'isOnline';
Queries with JPA CriteriaBuilder:
For the query in Java, the SQL query can also be created as a function and then used via the CriteriaBuilder.
CREATE OR REPLACE FUNCTION get_jsonb_attribute_value_function(jsonb_column jsonb, p_name text)
RETURNS TEXT AS $
BEGIN
RETURN (
SELECT jsonb_element->>'value'
FROM jsonb_array_elements(jsonb_column) AS jsonb_element
WHERE jsonb_element ->> 'key' = p_name LIMIT 1
);
END;
$ LANGUAGE plpgsql;
// Verwenden der SQL-Funktion get_jsonb_attribute_value_function mit CriteriaBuilder
Predicate attributePredicate = cb.equal(
cb.function(
"get_jsonb_attribute_value_function",
String.class,
root.get("deviceAttributes"),
cb.literal(attributeName)
),
attributeValue
);
query.where(attributePredicate);
Conclusion:
The integration of JSON and JSONB into relational databases offers developers and architects a valuable opportunity to work more flexibly with varying and unstructured data. While JSON and JSONB increase the freedom of structuring, JSONB offers clear performance advantages through better query performance and indexing. A sound understanding of the differences and areas of application of both data types makes it possible to make optimum use of the advantages of modern databases and create a powerful, flexible architecture.


