Skip to main content

Creating JSON

JSON Creation Functions

The following functions are used to create JSON.

FunctionDescription
to_json(any)Create JSON from a value of any type. Our LIST is converted to a JSON array, and our STRUCT and MAP are converted to a JSON object.
json_quote(any)Alias for to_json.
array_to_json(list)Alias for to_json that only accepts LIST.
row_to_json(list)Alias for to_json that only accepts STRUCT.
json_array(any, ...)Create a JSON array from the values in the argument lists.
json_object(key, value, ...)Create a JSON object from key, value pairs in the argument list. Requires an even number of arguments.
json_merge_patch(json, json)Merge two JSON documents together.

Examples:

Query
SELECT to_json('mercury');
Result
 to_json----------- "mercury"
Query
SELECT to_json([1, 2, 3]);
Result
 to_json--------- [1,2,3]
Query
SELECT to_json({mercury : 42});
Result
 to_json---------------- {"mercury":42}
Query
SELECT to_json(MAP(['mercury'], [42]));
Result
 to_json---------------- {"mercury":42}
Query
SELECT json_array('mercury', 42, 'venus', 123);
Result
 json_array---------------------------- ["mercury",42,"venus",123]
Query
SELECT json_object('mercury', 42, 'venus', 123);
Result
 json_object---------------------------- {"mercury":42,"venus":123}
Query
SELECT json_merge_patch('{"mercury": 42}', '{"venus": 123}');
Result
 json_merge_patch---------------------------- {"mercury":42,"venus":123}

This page contains: