Skip to main content
Version: V1.0.0

JSON_PRETTY

Description

This function prints a JSON value in a formatted manner. It is similar to the functionality in PHP.

Syntax

JSON_PRETTY(json_val)

Description

The json_val parameter must be a JSON value or a valid string representation of a JSON value. Any extraneous spaces or line breaks within this value do not affect the output. If the value is not a valid JSON document or cannot be parsed, the function will fail and display an error.

For NULL values, the function returns NULL.

The output format of this function follows these rules:

  • Each array element or object member appears on its own line, indented one level deeper than its parent element.

  • Each level of indentation adds two leading spaces.

  • The comma separating individual array elements or object members is printed before the newline character that separates two elements or members.

  • The key and value of an object member are separated by a colon followed by a space (': ').

  • Empty objects or arrays are printed on a single line. No space is printed between the left and right brackets.

  • Special characters in string scalars and key names use the same escaping rules as the JSON_QUOTE() function.

Examples

SELECT JSON_PRETTY('1234');
+--------------------+
| JSON_PRETTY('1234') |
+--------------------+
| 1234 |
+--------------------+
1 row in set (0.001 sec)

SELECT JSON_PRETTY("[1,3,5,7]");
+--------------------------+
| JSON_PRETTY("[1,3,5,7]") |
+--------------------------+
| [
1,
3,
5,
7
] |
+--------------------------+
1 row in set (0.001 sec)

SELECT JSON_PRETTY('{"a":"10","b":"20","c":"30"}');
+---------------------------------------------+
| JSON_PRETTY('{"a":"10","b":"20","c":"30"}') |
+---------------------------------------------+
| {
"a": "10",
"b": "20",
"c": "30"
} |
+---------------------------------------------+
1 row in set (0.001 sec)