-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding json convertion on mysql select statement
- Loading branch information
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# How to select columns as json | ||
|
||
Its possible to transform several columns into a json by using the following function. | ||
|
||
```sql | ||
SELECT | ||
id, | ||
JSON_OBJECT ( | ||
"id", id, | ||
"name", full_name, | ||
"created_date", creationdate, | ||
"details", JSON_OBJECT( | ||
"address", address_column, | ||
"country", country | ||
) | ||
) as json_data | ||
FROM table_name; | ||
``` | ||
|
||
Generated json: | ||
```json | ||
{ | ||
"id": 1, | ||
"name": "full name of someone", | ||
"created_date": "2022-01-31 00:00:00", | ||
"details": { | ||
"address": "the address of this someone", | ||
"country": "country code" | ||
} | ||
} | ||
``` |