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

Is it posible to use multiple values from a json list in a mysql where clause?

I have this table filled with values, and it's all structured in JSON. PersonID ValueID Value 1 1 {"Values":[{"ID":1,"Value":true},{"ID":2,"Value":true}]} 1 2 {"Values":[{"ID":2,"Value":false},{"ID":3,"Value":true}]} So I was wondering…
fustrax
  • 3
  • 1
  • 4
0
votes
1 answer

Get element value in a mysql nested json data

What would be the right way of getting Ajax, i.e. the value for the last occurence for key child1Dob1, from a json field that has a data structure that looks like the below, { "data": { "data": { "data": { "child1Dob1": "Andy" …
Ayubx
  • 597
  • 2
  • 9
  • 19
0
votes
0 answers

Spring JPA : Want to add key-value pair in json column

I am using spring jpa(spring-data-jpa-1.11.4) with mysql connector-5. In mysql, I have a table named chat_templates with column cta_data of json type. Use Case :I want to add a new key-value pair in existing json of mysql column. Observations: I…
0
votes
1 answer

Delete Data JSON from table MySql

Hi I need to remove from a json table everything that contains the name weapon_pistol50 , this is one of my tables in…
0
votes
1 answer

How to search within MySQL JSON object array?

Consider the following JSON object, [ { "id": 5964460916832, "name": "Size", "value": "Small", "position": 1, "product_id": 4588516409440 }, { "id": 5964460916833, "name": "Size", "value": "Medium", …
Stranger
  • 10,332
  • 18
  • 78
  • 115
0
votes
1 answer

Update Mysql Column of JSON array via SQL

I am storing a list of integers as JSON array inside the column called ConvertedIds inside a table SelectionLogs The type of column is MediumText and some of the example values of the column are …
Sebastian
  • 4,625
  • 17
  • 76
  • 145
0
votes
1 answer

Print key, value pairs from nested MYSQL json

I have extracted a mysql json dictionary strucutre and I wish to get all the values associated with the keys alpha and beta; however I also wish to print the key too. The structure of the dictionary is: results = {1: {"a": {"alpha": 1234, …
Wychh
  • 656
  • 6
  • 20
0
votes
0 answers

How to retrieve data as json in fields from mysql

this question may be weird or maybe unknown to me so i wanted to make sure is it possible or not . this is very basic query select * from table; and the output is like id, column1, column2, . . . okay what i was thinking about json on mysql is…
user14291527
0
votes
2 answers

Unable to extract mysql json data while using where clause

I have a table with the below json data type column in a table TABLESDATA create table tablesdata(sample json); And I am using the sql to query the table. select sample->>"$[*].name" as new_data from tablesdata WHERE sample ->>'$[*].name' =…
Chinna
  • 1
  • 2
0
votes
0 answers

How to add, insert and update JSON value into MYSQL?

I have one JSON column ('posts') in MYSQL table . When somebody posts something i want to add this data to this column like this { "status": "active", "id": "112" } I can only do this "UPDATE users set posts = ? WHERE id = ? " But it deletes…
0
votes
1 answer

How to manage JSON query performance in MySQL DB

I have a Mysql8 DB which contains JSON data. Unfortunately, the content is not always the same. To make it simple, the hierarchy is always the same, but sometimes part of the "tree" is missing or slightly different. For…
ChezYom
  • 11
  • 1
  • 5
0
votes
1 answer

MySQL JSON Query: Invalid JSON text in argument 1

I am building a stats table that tracks user data points. The JSON is dynamic and can grow for multiple levels. I'm basically getting an error about invalid JSON using json_merge_patch, which I have used often before. I can not figure out why this…
RJW
  • 21
  • 4
0
votes
1 answer

SQL: json_extract a key from a every field in a list

Hey I have a table with two columns - A and B. Column A is a VARCHAR. Column B has stringified list of jsons. The size of the list varies. Every JSON has a key called "foo". As a part of a query, I want to check if A = any of the foo values So…
Sid Anand
  • 167
  • 1
  • 10
0
votes
3 answers

Check if boolean value present in nested object

I have a JSON column and the data stored looks like: {"results":{"made":true,"cooked":true,"eaten":true}} {"results":{"made":true,"cooked":true,"eaten":false}} {"results":{"made":true,"eaten":true,"a":false,"b":true,"c":false}, "more":…
PeterB
  • 2,212
  • 2
  • 21
  • 33
0
votes
1 answer

MYSQL JSON ordering by values

I am using a JSON type to store some data in a table, and I'm having some trouble ordering said data while I'm fetching it. Example Data: {"574161434674462720":1,"870013663581437952":6,"903739315782320168":3,"913739315950071829":1} The json here is…
Saratoubi
  • 3
  • 2