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_object and json_arrayagg with group by not working as expected

I've a many to many relationship with this tables Table assets: id name other fields 19 asset 1 ... 20 asset 2 ... ... ... ... Branches table: id name other fields 1 branch 1 ... 2 branch…
leandronn
  • 173
  • 1
  • 17
1
vote
2 answers

Convert json column content key value pairs to separated key value

My table looks like this: id data 1 {tags: {"env": "dev", "owner": "me"}} I want to fetch the data and inside the select query convert the data column to the following format: id data 1 {tags: [{"key": "env", "value": "dev"},…
BenLa
  • 27
  • 1
  • 6
1
vote
1 answer

Nested JSON in MySQL with filtering

Here's the JSON that I have in the MySQL table: { "qmSubsegmentFlags": [ { "text": "Accuracy", "id": 1, "children": [ { "text": "Mistranslation", …
Remy
  • 12,555
  • 14
  • 64
  • 104
1
vote
1 answer

Can't get through mysql JSON_CONTAINS to show the correcct data. Getting null everytime i try

The data in the table events with column attributes is in json and looks like this…
1
vote
0 answers

How to preserve a json_array when creating a JSON object using json_objectagg in MySQL?

I'm trying to build a JSON object with some data that I need aggregated. I first aggregate values into an array, and then I aggregate them into a json object. I have not been able to figure out how to prevent it from turning the JSON array into a…
shakeman
  • 21
  • 2
1
vote
1 answer

Select data from json object using mysql

I have a table containg two columns: Customer id json_data (it contains JSON object) is as follow { "nameValuePairs": { "CONTACTS": { "nameValuePairs": { "contacts": { "values": [ { …
1
vote
1 answer

Return a MySQL result, with results formated as JSON, with row id as the JSON key for each row result

A query like this: SELECT JSON_ARRAYAGG(JSON_OBJECT('id', id, 'tag', tag)) from all_tags; produces a result like this: [ { "id": 979, "tag": "alternative" }, { "id": 947, "tag": "ambient" } ] The…
shawn caza
  • 342
  • 2
  • 13
1
vote
1 answer

Unnest key value pairs from json object

I have a map with multiple key value pairs and I want to unnest them into separate rows. "dayValueMap": { "2022-06-01": 1, "2022-06-02": 1, "2022-06-03": 1, "2022-06-04": 1, "2022-06-05": 1, "2022-06-06": 1, } I tried…
1
vote
1 answer

How can I select all possible JSON Data in arrow syntax/JSON Extract in SQL

I need to be able to access all the available JSON data, the problem is that a lot of it is nested. I currently have this query. SELECT * FROM `system_log` WHERE entry->"$[0]" LIKE "%search_term%"; I need instead of entry->"$[0]", something like…
HJP22
  • 33
  • 4
1
vote
1 answer

Why is mysql JSON_OBJECT variable assignment inconsistent?

Can someone help explain the following behavior? As near as I can tell, it looks like when a JSON object is assigned to a mysql variable, it may or may not be escaped depending upon the context. So let's try it mysql> CREATE TABLE `json_test`…
Mike Patnode
  • 416
  • 5
  • 14
1
vote
1 answer

JSON column or nullable fields?

I have a number of fields that are applicable to some rows but not others. Instead of making individual fields and making them null for particular rows, I am considering making a JSON field and storing the data in there. Would there be a performance…
panthro
  • 22,779
  • 66
  • 183
  • 324
1
vote
1 answer

right way to use calculated value in stored procedure with mysql

I have a JSON object in my DataBase { "3a232f53-7837-49ff-85af-488f6f233ba0" : { "exam" : 15, "tools" : 5, "practice" : 30, "intro" : 5, "dictionary" : 5, "class" : 6 }, "2fa94eb2-7f6a-4c59-80d1-e63338be2121" : { …
1
vote
0 answers

Transform a JSON array of objects with MySQL 5.7 (map)

I'm stuck with MySQL 5.7 and to reduce the amount of data retrieved I need to transform a JSON array within a SQL query. My column is JSON and with a value structured like that: { "foo": [{ "bar" : "BAR1", "baz" : "BAZ1", "qux" :…
Yves M.
  • 29,855
  • 23
  • 108
  • 144
1
vote
2 answers

querying a JSON array of integers for the presence of a value in MySQL 5.7

I have a MySQL 5.7 table that looks like this: id meta 1 null 2 {"grant_ids": [5, 7]} I'd like to query for rows that have the presence of the value of 5 in meta-> grant_ids. I tried select * from content.banners where…
timpone
  • 19,235
  • 36
  • 121
  • 211
1
vote
1 answer

Mysql explode json array to rows

From a table with a column with json array of dictionaries i need to extract all values of key "user_id" one per row. If null or empty array return NULL. Similar to python pandas explode method. Length of array is unknown. Original table: | id |…