
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to create JSON format with group-concat in MySQL?
You can create JSON format using group_concat() function from MySQL. The syntax is as follows −
SELECT yourColumnName1, GROUP_CONCAT(CONCAT('{anytName:"', yourColumnName, '", anyName:"',yourColunName,'"}')) anyVariableName from yourTableName group by yourColumnName1;
To understand the above syntax, let us first create a table. The query to create a table is as follows −
mysql> create table JsonFormatDemo -> ( -> UserId int, -> UserName varchar(100), -> UserEmail varchar(100) -> ); Query OK, 0 rows affected (0.99 sec)
Insert some records in the table using insert command. The query to insert record is as follows −
mysql> insert into JsonFormatDemo values(101,'John','John@gmail.com'); Query OK, 1 row affected (0.19 sec) mysql> insert into JsonFormatDemo values(101,'Bob','John@gmail.com'); Query OK, 1 row affected (0.18 sec) mysql> insert into JsonFormatDemo values(102,'Carol','Carol@gmail.com'); Query OK, 1 row affected (0.12 sec) mysql> insert into JsonFormatDemo values(103,'Sam','Sam@gmail.com'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from JsonFormatDemo;
Output
+--------+----------+-----------------+ | UserId | UserName | UserEmail | +--------+----------+-----------------+ | 101 | John | John@gmail.com | | 101 | Bob | John@gmail.com | | 102 | Carol | Carol@gmail.com | | 103 | Sam | Sam@gmail.com | +--------+----------+-----------------+ 4 rows in set (0.00 sec)
The query to create a JSON format with the help of group_concat() function −
mysql> select UserId, -> GROUP_CONCAT(CONCAT('{Name:"', UserName, '", Email:"',UserEmail,'"}')) JsonFormat -> from JsonFormatDemo -> group by UserId;
Output
+--------+----------------------------------------------------------------------------+ | UserId | JsonFormat | +--------+----------------------------------------------------------------------------+ | 101 | {Name:"John", Email:"John@gmail.com"},{Name:"Bob", Email:"John@gmail.com"} | | 102 | {Name:"Carol", Email:"Carol@gmail.com"} | | 103 | {Name:"Sam", Email:"Sam@gmail.com"} | +--------+----------------------------------------------------------------------------+ 3 rows in set (0.00 sec)
- Related Articles
- Update a MySQL column with JSON format?
- Getting last value in MySQL group concat?
- MySQL query to group concat distinct by Id?
- How to convert MySQL DATETIME value to JSON format in JavaScript?
- Convert JSON to another JSON format with recursion JavaScript
- How to format JSON string in JavaScript?
- How to group JSON data in JavaScript?
- MySQL GROUP BY and CONCAT() to display distinct first and last name
- How to convert Python date in JSON format?
- MySQL concat() to create column names to be used in a query?
- How to use GROUP_CONCAT in CONCAT in MySQL?
- How can we create a MySQL view with GROUP BY clause?
- Transform tree from DB format to JSON format in JavaScript
- How to print Python dictionary into JSON format?
- How to change date format with DATE_FORMAT() in MySQL?

Advertisements