Converts JSON data to a STRING value. No pretty-print formatting is applied by default.
Syntax
STRING JSON_FORMAT(<json>)
Parameter
| Parameter | Required | Type | Description |
|---|---|---|---|
json |
Yes | JSON | The JSON data to convert. |
Return value
Returns a STRING value.
Usage notes
-
Key order: Keys in a JSON object are sorted alphabetically in the output. Do not rely on a specific key order in downstream logic.
-
Compact output: No whitespace or indentation is added. Apply formatting in your application layer if you need pretty-printed output.
Examples
The following examples show how JSON_FORMAT handles each JSON value type. All inputs use the JSON '<literal>' syntax.
SELECT JSON_FORMAT(JSON '123'); -- 123
SELECT JSON_FORMAT(JSON '"123"'); -- "123"
SELECT JSON_FORMAT(JSON '{"name":"Alice","age":3,"school":"Ali"}'); -- {"age":3,"name":"Alice","school":"Ali"}
The JSON object example shows that keys are sorted alphabetically (agebeforenamebeforeschool), regardless of the original input order.
The following example converts a JSON array of objects to a string.
SELECT JSON_FORMAT(
JSON
'[
{"id": 1, "name": "Bob", "score": 98},
{"id": 2, "name": "Alex", "score": 88}
]'
);
Return result:
+------------------------------------------------------------------------+
| _c0 |
+------------------------------------------------------------------------+
| [{"id":1,"name":"Bob","score":98},{"id":2,"name":"Alex","score":88}] |
+------------------------------------------------------------------------+
Related functions
JSON_FORMAT is a complex type function. For more information about functions that process complex data types, such as ARRAY, MAP, STRUCT, and JSON data, see Complex type functions.