From c96d46f8edc4b0af628295152fd427eee108fee4 Mon Sep 17 00:00:00 2001 From: Kelvin Ferreira <10376375+k30v1n@users.noreply.github.com> Date: Mon, 31 Jan 2022 14:42:01 -0500 Subject: [PATCH] adding json convertion on mysql select statement --- README.md | 1 + mysql/how_to_select_columns_as_json.md | 31 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 mysql/how_to_select_columns_as_json.md diff --git a/README.md b/README.md index a203653..adb6433 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/mysql/how_to_select_columns_as_json.md b/mysql/how_to_select_columns_as_json.md new file mode 100644 index 0000000..dd3e1a3 --- /dev/null +++ b/mysql/how_to_select_columns_as_json.md @@ -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" + } +} +```