MariaDB JSON_COMPACT() Function

In MariaDB, JSON_COMPACT() is a built-in function that removes all unnecessary spaces from a JSON document to reduce the size of the JSON document.

JSON_DETAILED() is opposite to this function, it prettifies the JSON document by adding necessary spaces in it for better readability.

MariaDB JSON_COMPACT() Syntax

Here is the syntax of the MariaDB JSON_COMPACT() function:

JSON_COMPACT(json)

Parameters

json

Required. The JSON document to be processed.

If you supply the wrong number of arguments, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_COMPACT'.

Return value

The MariaDB JSON_COMPACT() function remove unnecessary spaces from a given JSON document.

If you provide a NULL value as an argument, JSON_COMPACT() it will be returned NULL.

MariaDB JSON_COMPACT() Examples

The following example shows the usages of the MariaDB JSON_COMPACT() function.

Example 1

SET @json_doc = '{
  "x": 1,
  "b": [2,  3]
}';

SELECT JSON_COMPACT(@json_doc);

Output:

+-------------------------+
| JSON_COMPACT(@json_doc) |
+-------------------------+
| {"x":1,"b":[2,3]}       |
+-------------------------+

In this example, all deletable whitespace and newlines in the given JSON document are removed by JSON_COMPACT().

NULL parameters

If you provide a NULL value as an argument, JSON_COMPACT() will return NULL.

SELECT JSON_COMPACT(NULL);

Output:

+--------------------+
| JSON_COMPACT(NULL) |
+--------------------+
| NULL               |
+--------------------+

Conclusion

In MariaDB, JSON_COMPACT() is a built-in function that removes all unnecessary spaces from a JSON document to reduce the size of the JSON document.