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
votes
1 answer

Mysql, How do I add a computed column to compute the sum of a json column?

Column a contains a json array formatted like [1,4,3,6] I want column b to be a sum of column a. ALTER TABLE `T` ADD `b` int AS (SELECT SUM(t.c) FROM JSON_TABLE(a, '$[*]' COLUMNS (c INT PATH '$')) AS t) NULL; 1064 - You have an error in…
-1
votes
1 answer

compare two MySQL tables with different types of data - JSON string array vs strings

Table A has one column 'phoneNumber' type string, Table B with column 'number' type JSON. table B has one record with the value json array of phoneNumbers: '["052669988", "0526635444"]'. what is the best way to select all the values that Exist in…
July
  • 67
  • 7
-1
votes
1 answer

JSON_EXTRACT for mutliple values problem (Matching a string in a form of integer array to integer using MYSQL)

table1: id languages 2 ["2"] 3 ["1"] 9 ["1","2"] "languages" field is a varchar(255). Table2: language_id name 1 English 2 Japanese "language_id" is int(11). I want output like one of the following: Desired Output…
online.0227
  • 640
  • 4
  • 15
  • 29
-1
votes
1 answer

Is there a way in MySQL to loop through nested JSON and update them?

For example, I have a field like the following, and I want to update all the current values to the initial values. { "dataset": { "field1": { "current": "Customized", "initial": "Initial1" }, "field2": { "current":…
user2335065
  • 2,337
  • 3
  • 31
  • 54
-1
votes
2 answers

mySQL json array reformatting

I have a table which contains a column "owners", which has json data in it like this: [ { "first":"bob", "last":"boblast" }, { "first":"mary", "last": "marylast" } ] I would like to write a query that would…
bpeikes
  • 3,495
  • 9
  • 42
  • 80
-1
votes
1 answer

How to deal with not existing values using JSON_EXTRACT?

I have a list ob objects. Each object contains several properties. Now I want to make a SELECT statement that gives me a list of a single property values. The simplified list look like this: [ [ { "day": "2021-10-01", …
Lars
  • 920
  • 1
  • 14
  • 34
-1
votes
2 answers

Mysql join on json value issue

i have the folowwing tables on mysql 5.7 server produit__parure id_parure (int) ids_produits_parure (json) 21 ["34809", "34823", "34813"] 22 ["35703", "35854", "35877"] and produit : id_product (int) ... other…
MicMoz
  • 11
  • 4
-1
votes
1 answer

LEFT JOIN between two mySQL JSON Datastore queries

I have two queries that work to retrieve data from a mySQL JSON Datastore fieldtype. The trick is I need to somehow LEFT JOIN them, and the typical relational-database method doesn't seem to work. The two queries I need to somehow join are: SELECT…
user1258530
  • 99
  • 2
  • 11
-2
votes
1 answer

MYSQL:: Stored Procedure where condition is not working

i am trying to build json object from MYSQL select and pass it to out param. everything is working fine, but my where condition is not applying when i am using json_object, json_arrayagg, json_objectagg. my select query is, SELECT …
-3
votes
1 answer

MYSQL JSON how i can get all name and value from table attributes

{ "1559a633-9037-11ed-a63f-000c292e0bd3": { "name": "speed", "value": "230" }, "23d1e822-90c1-11ed-a63f-000c292e0bd3": { "name": "power", "value": "12v" }, } i try SELECT attributes->'$.name' AS name…
-4
votes
0 answers

Unable to upload the product with the error Object of class stdClass could not be converted to string

This is we are using at the backend to upload the product in to our website and when we click on the product page, we are getting error of Object of class stdClass could not be converted to string Because of this we are not able to upload the…
1 2 3
25
26