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
2
votes
2 answers

can I compare with where two entire json values?

I have a JSON field in the stadiums table (mysql) named location, can i compare it with another json like below?... select *from stadiums where location = '{"lat":40, "lng":3}'; *The query doesnt return me any error, but doesnt return me any row…
2
votes
0 answers

How to index a property inside an array or nested object in a JSON data type?

Generating a virtual column is the recommended way to index a JSON property. But what if the property I want to index is inside an array or object? For example, if the JSON property is an array of userIds, and I'm querying for the userId in the…
Glide
  • 20,235
  • 26
  • 86
  • 135
2
votes
1 answer

Issue with searching for object property in array for JSON data type

I have the following schema with value as JSON type mysql> select * from people; +------+---------------------------------------------------------------------------+ | id | value …
Glide
  • 20,235
  • 26
  • 86
  • 135
2
votes
1 answer

JSON_REMOVE for array.array results in "path expressions may not contain the * and ** tokens" Error

If I do: SELECT JSON_REMOVE(@I, '$.friends[*].name'); OR SELECT JSON_REMOVE(@I, '$.friends[*].friends'); on the JSON below, I get this this error: ERROR 3149 (42000): In this situation, path expressions may not contain the * and ** tokens. JSON:…
Glide
  • 20,235
  • 26
  • 86
  • 135
2
votes
1 answer

MySQL - How to search within JSON datafield

I am using MYSQL 5.7.11 which supports JSON datafields. I have a JSON field in one of my tables that stores price history of a product: JSON structure Sample: [{"da": "2016-05-03 08:32", "pr":15.90}] [{"da": "2016-03-22 09:02", "pr":14.40}] [{"da":…
Dror
  • 1,262
  • 3
  • 21
  • 34
1
vote
2 answers

Comparing JSON-typed column with specific string value

I have following table: CREATE TABLE mytable ( id int, name varchar(255), data json ); id name data 1 John ["a", "b"] 2 Pete ["a", "b", "c"] 3 Mike ["a", "b"] Where column data has JSON type, and I want to…
Vaks
  • 15
  • 4
1
vote
1 answer

How do I update a data value stored in an array of JSON data stored in MySQL dB

I have JSON data in an array in a dB. I would like to update the value for one field. This returns the array: SELECT id, data from ComponentResult WHERE id = 2272; This returns the last element of this array: SELECT id, json_extract(data,…
jason
  • 11
  • 2
1
vote
2 answers

MYSQL json query where array is present in json field likes

I wonder if this query is possible using json query. I need to select all records that have more than 3 values a like. for example, I have an array [1,2,3,4,5,6]. Database record has: # ["1","2","3","9","4","2"] selected <--- this contains…
1
vote
1 answer

MySQL how do I use JSON_SET to set a certain element of a JSON array?

I have a table called "completedgames" it has two columns "GameID" and "gameresults" I have this JSON inside the "gameresults" column { "gameID": "acaa2a99-a24c", "scores": [ { "id": "11888572", "seen":…
1
vote
1 answer

MySQL accessing a json array

Need help figuring out syntax for an MySQL query. I have a table called "activegames" with a column called "gameresults" inside game results I have this JSON data. { "gameID": "7c3b0c36-c18e", "scores": [ { "id":…
1
vote
1 answer

How do we get data from the user in json format with request.form with Flask?

I am making a user management system application, a user can have more than one admin, so one of my columns in mysql is in json format, but how do I update this column, how do I add it? When I try to update the request.form with the data I get from…
1
vote
1 answer

in mysql, how to delete elements in json array that don't contain certain string values?

I have table with a column of jsons that have example value: {"name": "1", "list": ["abc", "xyz"]} {"name": "2", "list": ["b", "bc", "bcd"]} {"name": "3", "list": ["cd", "cdef", "def", "defg", "ef"]} I also have a array of (sub)strings that I want…
이준서
  • 35
  • 5
1
vote
1 answer

MySQL update run SET for every row from subquery

I have two tables: Files and FileLogs and I'm trying to get rid of the FileLogs table and store some data from it inside the Files table. Files id lastStatusId meta 187 101 NULL 188 101 { "foo": "bar"…
Philipp Kyeck
  • 18,402
  • 15
  • 86
  • 123
1
vote
1 answer

sql concatenate values of an array of jsons

In mysql, I have a table with a json column of example data: { "fruits": [ {"id": 1, "name": "apple", "amount": 3}, {"id": 3, "name": "banana", "amount": 5} ] } how to i get a column of concatenated values of fruit…
이준서
  • 35
  • 5
1
vote
1 answer

JSON_CONTAINS doesn't work in mysql query as expected

I'm trying to query over a table that has a JSON field (array of integers) like this {"1": 45, "2": 46}, I expected the below query returns all the rows that have 46 in their room_category field: SELECT * FROM rooms WHERE…
Fatemeh Rostami
  • 1,047
  • 1
  • 15
  • 27