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
157
votes
10 answers

How to search JSON data in MySQL?

I have inserted records in mysql DB, with json encoded data type, Now I have to make search within json encoded data, but i am not able to get proper data using following MySql query. SELECT `id` , `attribs_json` FROM `products` WHERE …
reza
  • 1,555
  • 2
  • 10
  • 10
103
votes
2 answers

How to update JSON data type column in MySQL 5.7.10?

I have started using MySQL 5.7.10 recently and I am liking the native JSON Data type a lot. But I ran into a problem when it comes to updating a JSON type value. Questions: Below is the table format, here I want to add 1 more key in JSON data column…
ʞɹᴉʞ ǝʌɐp
  • 5,350
  • 8
  • 39
  • 65
98
votes
11 answers

How to search JSON array in MySQL?

Let's say I have a JSON column named data in some MySQL table, and this column is a single array. So, for example, data may contain: [1,2,3,4,5] Now I want to select all rows which have a data column where one of its array elements is greater than…
user1011792
75
votes
11 answers

JSON vs. Serialized Array in database

What are the advantages and disadvantages of storing JSON data in MySQL database vs. serialized array?
Inez
  • 2,115
  • 3
  • 20
  • 25
72
votes
2 answers

Should I use blob or text for JSON in MySQL?

I am planning to store a json_encoded string on my database. I can't precisely tell the length its going to be, but I'm pretty sure it will be long. My concern is which field type I am going to use for this, is it blob or text? I prefer the one…
Leandro Garcia
  • 3,138
  • 11
  • 32
  • 44
67
votes
12 answers

MySQL 5.7.12 import cannot create a JSON value from a string with CHARACTER SET 'binary'

I exported my database with JSON columns in it. After I migrated to a new server, my import crashed every time with an error like: cannot create a JSON value from a string with CHARACTER SET 'binary' On stackoverflow, I found this post but didn't…
Danny Bevers
  • 831
  • 1
  • 8
  • 16
33
votes
4 answers

Query a JSON column with an array of object in MySQL

I have a json column with the follow array: [ { "id": "24276e4b-de81-4c2c-84e7-eed9c3582a31", "key": "id", "type": "input", }, { "id": "e0ca5aa1-359f-4460-80ad-70445be49644", "key": "name", "type": "textarea", …
Fabrício
  • 1,060
  • 1
  • 12
  • 17
25
votes
6 answers

Using MySQL JSON field to join on a table

I have a json field that stores a list of ids (not best practice here I know), I want to know if it's possible to use do operations on this JSON field and use them in the sql. Below is a fictitious example of what I'm trying to achieve, is something…
Kyle Gobel
  • 5,530
  • 9
  • 45
  • 68
18
votes
2 answers

Chaining JSON_EXTRACT with CAST or STR_TO_DATE fails

I'm trying to extract a datetime from a JSONFIELD "data" in MySQL. If I do a simple JSON_EXTRACT however, the return field type is a JSON. mysql> select JSON_EXTRACT(data, "$.new_time") from analytics limit 10; +----------------------------------+ |…
rtindru
  • 5,107
  • 9
  • 41
  • 59
18
votes
4 answers

MySQL 5.7+, JSON_SET value in nested path

For a recent development project, we're using MySQL 5.7, so we can take advantages of the latest JSON-functions... I'm building an UPDATE-query, where an nested json-object should be inserted / added into the attributes-column, of type JSON, see…
Bazardshoxer
  • 545
  • 1
  • 5
  • 22
18
votes
2 answers

Remove a key:value from json string stored in a MySQL database

I have a column in table which is stored in format: {"field1":"val1","field2":"val4"} {"field1":"val2","field2":"val5"} {"field1":"val3","field2":"val6"} I need to remove all field1 with values(e.g "field1":"val1","field1":"val2","field1":"val3" )…
ABC
  • 4,263
  • 10
  • 45
  • 72
16
votes
2 answers

MySQL nested JSON column search and extract sub JSON

I have a MySQL table authors with columns id, name and published_books. In this, published_books is a JSON column. With sample data, id | name | published_books ----------------------------------------------------------------------- 1 | Tina | …
Kanmaniselvan
  • 522
  • 1
  • 8
  • 23
15
votes
1 answer

How to extract values from a numeric-keyed nested JSON field in MySQL

I have a MySQL table with a JSON column called sent. The entries in the column have information like below: { "data": { "12":"1920293" } } I'm trying to use the mysql query: select sent->"$.data.12" from mytable but I get an exception:…
Ibrahim Lawal
  • 1,168
  • 16
  • 31
13
votes
2 answers

MYSQL JSON column change array order after saving

I am using JSON column type in MySQL database table. When I try to save JSON values in table column, the JSON array automatically re-order(shuffle) I have following JSON: {"TIMER_HEADER": [{"XX!TIMERHDR": "XXTIMERHDR", "VER": " 7", "REL": " 0",…
Rahul Dadhich
  • 1,213
  • 19
  • 32
12
votes
2 answers

mysql JSON_SET can't insert into column with NULL value(5.7+)

I am exploring the JSON funcions of newer mysql server. But run into a very rudimentary issue. How do I insert {"key":"value"} into a JSON column when I don't know if the current value is NULL or not? The table is for illustration only: CREATE…
Reed
  • 1,628
  • 3
  • 21
  • 29
1
2 3
25 26