Questions tagged [mysql-json]

DO NOT USE FOR BASIC JSON. Use for questions about the JSON data type in MySQL 5.7 or later. Use with the [mysql] tag.

Overview

MySQL 5.7.8 introduced a new native json data type.

This data type allows the developer to store and manipulate JSON-encoded data more efficiently, in comparison to JSON data stored as the more common string data type.

Insert

Data inserted must be valid JSON syntax

INSERT INTO t1 VALUES('{"key1": "value1", "key2": "value2"}');

Select

Retrieving data directly from a JSON column

mysql> SELECT JSON_EXTRACT('{"id": 14, "name": "Aztalan"}', '$.name');
+---------------------------------------------------------+
| JSON_EXTRACT('{"id": 14, "name": "Aztalan"}', '$.name') |
+---------------------------------------------------------+
| "Aztalan"                                               |
+---------------------------------------------------------+

Create table example

The following is a data-definition statement that defines an auto-generated JSON Object that reflects the corresponding values of the data row in the target table.

CREATE TABLE triangle (
  sidea     DOUBLE,
  sideb     DOUBLE,
  sidec     DOUBLE AS (SQRT(sidea * sidea + sideb * sideb)),
  rowdata   JSON AS (JSON_OBJECT("aa",sidea, "bb",sideb, "cc",sidec ))
);
INSERT INTO triangle (sidea, sideb) VALUES(1,1),(3,4),(6,8);    

## Table
1   1   1.4142135623730951  {"aa": 1, "bb": 1, "cc": 1.4142135623730951}
3   4   5   {"aa": 3, "bb": 4, "cc": 5}
6   8   10  {"aa": 6, "bb": 8, "cc": 10}

Basic functions

--- Query
SELECT JSON_Array(
  'alpha'      
  ,'bravo'    
  ,'charlie'
  ,'delta'
) AS Result;          

--- Result          
["alpha", "bravo", "charlie", "delta"]

--- Query
SELECT JSON_OBJECT(
  'alpha',      'one'
  ,'bravo',     'two'
  ,'charlie',   '3'
  ,'delta',     004
) AS Result;

--- Result          
{"alpha": "one", "bravo": "two", "delta": 4, "charlie": "3"}

See also

386 questions
1
vote
1 answer

MYSQL JSON search returns results in square brackets

I am conducting a simple search of JSON held in a MySQL database, however the results are returning enclosed in square brackets and quotes. SELECT stored_json->>'$.*.referal' AS referal FROM table WHERE id = 100 results in +------------+ | referal…
Jules
  • 275
  • 1
  • 4
  • 11
1
vote
0 answers

JSON data search in MySQL query

I have a MySQL column with below data: { "ids": [ "c9cfdecc-dfce-11eb-8c94-0ad691f61ea0", "c9cfdecc-dfce-11eb-8c94-0ad691f61ea1", "c9cfdecc-dfce-11eb-8c94-0ad691f61ea2" ] } How can I get records that matched with any requested…
Gowri
  • 1,832
  • 3
  • 29
  • 53
1
vote
1 answer

JOIN on JSON_KEYS

Tried making this work: SELECT tm.name FROM taxi t JOIN taxi_model tm ON JSON_CONTAINS(JSON_KEYS(t.info), CAST(tm.id AS JSON)) WHERE t.id = 1; Table Data: taxi ----------------------------------------- | id | info | …
kaskenades
  • 25
  • 6
1
vote
2 answers

MySQL JSON with arbitrary keys to table

There is a map nested in a large json payload like { "map": { "key1": "value1", "key2": "value2", "key3": "value3" }, // more stuff } I would like to generate a table like that: +------#--------+ | Key | Value …
Keks
  • 75
  • 4
1
vote
1 answer

In MySql, how do I check if a row with json field has a key that contains a specific text?

So let's say I have a row that looks like id json_data 1 { ... } 2 { ... } 3 { ... } I want to select the rows that have a key which contains a string abc. I tried to use something like SELECT * FROM table1 t where…
Dawn17
  • 7,825
  • 16
  • 57
  • 118
1
vote
1 answer

Is there any alternative to JSON_TABLE?

I have a table in MySQL with a column with json like this id col 1 [{"key": 1, "value": 9}, {"key": 2, "value": 8}, {"key": 3, "value": 7}, ...] 2 [{"key": 1, "value": 6}, {"key": 2, "value": 5}, {"key": 3, "value": 4}, ...] ... I need to…
Margot
  • 25
  • 7
1
vote
1 answer

Select value for a key in json in specific column in MySQL

In MySQL, how do I select ONLY the values for nationality key in the jsonInfo column for all rows? userid displayName jsonInfo 11 John Nakamura {"nationality":"ROK", "build": "average", "height":"511", "eyeColor":"brown", "ethnicity":"Asian"} 12…
user12738756
  • 119
  • 1
  • 8
1
vote
1 answer

Using JSON_TABLE to fetch a nested JSON array

I have a Json array while looks as below [{"keyId": "aded5b0107bb5a936604bcb6f", "keyNames": ["abc1, abc2"], "keyDesc": "test"}] I am using JSON_TABLE to fetch all the values and I have written the following query: SELECT j.keyId, j.keyNames,…
meallhour
  • 13,921
  • 21
  • 60
  • 117
1
vote
1 answer

Doing something wrong with JSON_EXTRACT and mysql local variable

Need some help with the MySQL query. I have a table named custom_fields with attributes id, user_id, name, value etc. name and values are of type string and it can store sometimes JSON also in the form of a string. Requirement: I need to list out…
Ajith
  • 325
  • 4
  • 17
1
vote
1 answer

Parsing JSON objects and storing in mysql variables

I am trying to parse a json array and its objects to further make use of them in my mysql procedure. But while reading the values it shows null. _list will contain [{"floor_id":"5","length":"40"},{"floor_id":"6","length":"61"}] Code: CREATE…
Ramaraju.d
  • 1,301
  • 6
  • 26
  • 46
1
vote
1 answer

MySQL with special character in the json path

I have a column in mysql which stores a column with json files and the the key of the json can contain any unicode characters. I have a query to calculate the cardinality of the specific key SELECT COUNT(DISTINCT…
dashenswen
  • 540
  • 2
  • 4
  • 21
1
vote
1 answer

Is there a way to pass a value or variable into json_path in Mysql

Is there a way to pass a value or variable into json_path in Mysql? I am trying to use json_table to get value from the header of my json and then get date in my content from the index that i got. In my code below, i am trying to passe the value…
Dannick Bedard
  • 373
  • 3
  • 14
1
vote
2 answers

Error when calling JSON_TABLE() in MySQL 5.7–compatible Amazon Aurora

I am getting the below error when trying to use the JSON_TABLE() function in MySQL 5.7–compatible Amazon Aurora. Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right…
Harsha W
  • 3,162
  • 5
  • 43
  • 77
1
vote
1 answer

How to extract and flatten and array that is inside of JSON objects in MySQL 8?

Given some json documents like this: { "_id":"00006073", "subscribersIds":[ 170968, 225647 ] } ----------------------------------- { "_id":"00006072", "subscribersIds":[ 170968 …
Jeff Mira
  • 25
  • 5
1
vote
1 answer

How to search a value in a nested array inside a JSON in Mysql 8?

Let's say I have a JSON object with the following structure: { "_id":"0000abcdefg", "type":"PP", "subscription":{ "subscribers":{ "physicSubscribers":[ { "civility":"M", …
Jeff Mira
  • 25
  • 5