Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How create json format with group-concat mysql?

How create json format with group-concat mysql?

(I use MySQL)

Example1:

table1:

email            |    name  |   phone ------------------------------------- [email protected]    | Ben      | 6555333 [email protected]    | Tom      | 2322452 [email protected]    | Dan      | 8768768 [email protected]    | Joi      | 3434356 

like syntax code that not give me the format:

select email, group-concat(name,phone) as list from table1  group by email 

output that I need:

email         |    list ------------------------------------------------ [email protected] |  {name:"Ben",phone:"6555333"},{name:"Joi",phone:"3434356"} [email protected] |  {name:"Tom",phone:"2322452"},{name:"Dan",phone:"8768768"} 

Thanks

like image 205
Ben Avatar asked Sep 20 '12 11:09

Ben


People also ask

What is JSON format in MySQL?

MySQL supports a native JSON data type defined by RFC 7159 that enables efficient access to data in JSON (JavaScript Object Notation) documents. The JSON data type provides these advantages over storing JSON-format strings in a string column: Automatic validation of JSON documents stored in JSON columns.

What is Json_arrayagg in MySQL?

JSON_ARRAYAGG returns a JSON array containing an element for each value in a given set of JSON or SQL values. It acts on a column or an expression that evaluates to a single value. Returns NULL in the case of an error, or if the result contains no rows. JSON_ARRAYAGG cannot currently be used as a window function.

What is the use of Group_concat in MySQL?

The GROUP_CONCAT() function in MySQL is used to concatenate data from multiple rows into one field. This is an aggregate (GROUP BY) function which returns a String value, if the group contains at least one non-NULL value. Otherwise, it returns NULL.

What is Json_objectagg?

JSON_OBJECTAGG( key , value ) Takes two column names or expressions as arguments, the first of these being used as a key and the second as a value, and returns a JSON object containing key-value pairs. Returns NULL if the result contains no rows, or in the event of an error.


2 Answers

With the newer versions of MySQL, you can use JSON_OBJECT function to achieve the desired result, like so:

GROUP_CONCAT(   JSON_OBJECT(     'name', name,     'phone', phone   ) ) AS list 

To get the SQL response ready to be parsed as an array:

CONCAT(   '[',   GROUP_CONCAT(     JSON_OBJECT(       'name', name,       'phone', phone     )   ),   ']' ) AS list 

This will give you a string like: [{name: 'ABC', phone: '111'}, {name: 'DEF', phone: '222'}] which can be JSON parsed. Hope this helps.

like image 131
myusuf Avatar answered Sep 26 '22 21:09

myusuf


Try this query -

SELECT   email,   GROUP_CONCAT(CONCAT('{name:"', name, '", phone:"',phone,'"}')) list FROM   table1 GROUP BY   email; 

JSON format result -

+---------------+-------------------------------------------------------------+ | email         | list                                                        | +---------------+-------------------------------------------------------------+ | [email protected] | {name:"Ben", phone:"6555333"},{name:"Joi", phone:"3434356"} | | [email protected] | {name:"Tom", phone:"2322452"},{name:"Dan", phone:"8768768"} | +---------------+-------------------------------------------------------------+ 
like image 42
Devart Avatar answered Sep 26 '22 21:09

Devart