Skip to content

Commit

Permalink
adding json convertion on mysql select statement
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinrfr committed Jan 31, 2022
1 parent 36da987 commit c96d46f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Repo to hold important notes, links and guides to help throughout the carrer
1. [How to create table with case insensitive columns](/mysql/how_to_create_table_with_case_insensitive_columns.md)
1. [How to insert blob](/mysql/how_to_insert_blob.md)
1. [How to load CSV file into a table](/mysql/how_to_load_csv_file_into_table.md)
1. [How to select columns as json](/mysql/how_to_select_columns_as_json.md)

## Powershell
1. [Powershell commands](/powershell/powershell_commands.md)
Expand Down
31 changes: 31 additions & 0 deletions mysql/how_to_select_columns_as_json.md
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"
}
}
```

0 comments on commit c96d46f

Please sign in to comment.