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

Extract value by key with double quote and backslash in json column

I have a table include data like this | id | jdoc | +----+-------------------------------------------+ | 1 | {"simple": "val1", "h\"simple": "val2"} | | 1 | {"simple": "val1", "h\"simple": "val2"} | | 1 |…
dashenswen
  • 540
  • 2
  • 4
  • 21
1
vote
2 answers

Search in Json in mysql 5.7

I want to search product id from JSON filed but I am not able to search from JSON filed. In JSON field value added in multilevel like the below image. I am able to search data from other filed like SELECT equip_id FROM ' . $table. ' where …
Pushpendra Singh
  • 182
  • 1
  • 1
  • 13
1
vote
1 answer

How to get length of an array in MySQL column?

I have an array in column Temp and I am trying to find the length of an array. I tried JSON_LENGTH but it's not working. So far I tried this SELECT JSON_LENGTH(Temp)from tbl_name; And got this Id Temp 1 ['53682', '66890', '53925', '54847'] 2 …
sheel
  • 467
  • 8
  • 23
1
vote
1 answer

MySql 8.0 - Filtered JSON query

I have a JSON blob that I am trying to extract a value from, specifically, the Zip Code under the Rating Information section (Expected value = 90703). Does MySql 8 support JSON filter expressions? JSON: { "quote_number": null, "items": [ …
JoeJ
  • 152
  • 2
  • 11
1
vote
1 answer

Update JSON data type column in MySql table

I have started using MySQL 8 and trying to update JSON data type in a mysql table My table t1 looks as below: # id group names 1100000 group1 [{"name": "name1", "type": "user"}, {"name": "name2", "type": "user"}, {"name": "techDept",…
meallhour
  • 13,921
  • 21
  • 60
  • 117
1
vote
2 answers

modify every object in json array in mysql

Json Array - [{"a":1},{"a":2}] Is there a way that I can update/set each object in this array with a single query ?. I can update an specific object using ` SELECT JSON_SET('[{"a":1},{"a":2}]','$[0].b','new val') which results [{"a": 1, "b": "new…
1
vote
1 answer

How to optimize this SQL query to filter on a longtext/JSON field

I have a table with a column of type longtext that stores nutrient values in JSON format. Here's an example of what the data looks…
Charles
  • 189
  • 1
  • 10
1
vote
1 answer

JSON in MySQL : get data from an object within a range of keys

What I have: A mysql table, say table1 table1 contains two columns viz. id and data id is int but the twist is data is JSON type and its keys are comparable the table1 contains only one row (for the sake of this…
Deepam Gupta
  • 2,374
  • 1
  • 30
  • 33
1
vote
2 answers

Update JSON nested values in Laravel Eloquent

I need to update a JSON's some of the nested values using Laravel Eloquent, But didn't get the exact result as I wanted as optimized. The below json needs to be updated frequently. I had a working solution to this, But needs to fine tune the…
Gunaseelan
  • 2,494
  • 5
  • 36
  • 43
1
vote
1 answer

A SQL query to filter through a stringified json array

I am struggling to write a sql query that can retrieve results from a table in a certain way. I have a table that may have the following type of data. In this table, values in antecendents column are sorted lists of skus in string form Id |…
Waqar Sadiq
  • 135
  • 1
  • 9
1
vote
3 answers

How would I query nested JSON data values in SQL using json_extract()?

Mysql version is 6.7+ Example of the column I need to get a value from below. json_extract(goal_templates.template_data, group_concat('$.resources')) ->This results in a NULL return for all rows. :template_data {"Resolve housing issues":…
Willy
  • 53
  • 1
  • 6
1
vote
1 answer

How to search through unknown property name in database with Laravel

I'm trying to search database with json contains method of laravel. Here is my JSON of one database line: { "row": { "1": { "ID":"110555175667" }, "2": { "ID":"11023235667" }, "3":…
sundowatch
  • 3,012
  • 3
  • 38
  • 66
1
vote
0 answers

MariaDB query JSON result escapes a double quote in text column as \\"

I'm converting an SQL query to JSON using something like: SELECT JSON_ARRAYAGG(JSON_OBJECT('field1', field1, ...)) FROM db.table One of my columns of string type contain simple ASCII strings with double quotes (") here and there. My problem is that…
meaning-matters
  • 21,929
  • 10
  • 82
  • 142
1
vote
2 answers

MariaDB JSON remove key and its values

I have a TABLBE like CREATE TABLE `saved_links` ( `link_entry_id` bigint(20) NOT NULL AUTO_INCREMENT, `link_id` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, `user_data_json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, PRIMARY…
ErickBest
  • 4,586
  • 5
  • 31
  • 43
1
vote
2 answers

MariaDB JOIN based on JSON array

I'm pretty sure this can be done, but after searching MariaDB docs, I'm not sure how. I would like to return all items based on a join_extract search (I think). These are the values to return from the vendor_certs table: +----+-------------------+ |…
Dario Zadro
  • 1,143
  • 2
  • 13
  • 23