Description
This function prints a JSON value in a formatted manner, similar to 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. Irrelevant spaces and line breaks in this value have no effect on the output. If the value is not a valid JSON document or cannot be parsed, the function will fail and an error will be returned.
For NULL values, the function returns NULL.
The output format of this function follows these rules:
Each array element or object member is placed on a separate line, indented one level relative to its parent element.
Add two leading spaces for each level of indentation.
The commas that separate individual array elements or object members are printed before the line breaks that separate 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 one line. No spaces are printed between the left and right parentheses.
Special characters in string scalars and key names are escaped using the same rules as the JSON_QUOTE() function.
Examples
obclient> SELECT JSON_PRETTY('1234');
+--------------------+
| JSON_PRETTY('1234') |
+--------------------+
| 1234 |
+--------------------+
1 row in set
obclient> SELECT JSON_PRETTY("[1,3,5,7]");
+--------------------------+
| JSON_PRETTY("[1,3,5,7]") |
+--------------------------+
| [
1,
3,
5,
7
] |
+--------------------------+
1 row in set
obclient> 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