MySQL can produce nested JSON objects using the JSON_OBJECT function. This function allows you to build a JSON object by combining key/value pairs. When constructing the JSON object, you can specify a nested structure by passing another JSON object as the value for a key. This process can be repeated for building a nested JSON object.
For example, the following MySQL query produces a nested JSON object:
SELECT JSONOBJECT( 'name', 'John', 'age', 30, 'contacts', JSONOBJECT( 'email', 'john@example.com', 'phone', '+1 123-456-7890' ) ) as user;
This query produces the following JSON object:
{ "name": "John", "age": 30, "contacts": { "email": "john@example.com", "phone": "+1 123-456-7890" } }
In this example, the "contacts" key has a value that is itself a JSON object, thus creating a nested structure.
Asked: 2022-12-26 11:00:00 +0000
Seen: 16 times
Last updated: Dec 16 '22