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

How to convert JSON object rows into JSON array in MySQL?

Stuck in one exercise task where I want JSON objects to be in form of a JSON array. Actually I have reached up to 1st stage to convert things into JSON object first. Second stage is where my output should be as same as expected output as below, but…
1
vote
1 answer

query JSON array column in mysql into rows

I have a table with data like | user_id | favorite_foods | |--------- |-------------------------------------- | | user1 | ["milk","cake"] | | user2 | null | |…
Anthony
  • 36,459
  • 25
  • 97
  • 163
1
vote
1 answer

how to extract and zip values from json with SQL, mysql

I struggle to extract the required values from the JSON column in MySQL. Imagine the variable: SET @j = '[{"a": 1, "b": 2},{"a": 3, "b": 4, "c": {"d": 4}}, {"a": 5}]'; and I need to extract and return a new JSON array containing only selected…
Luke
  • 35
  • 4
1
vote
2 answers

How to update all string into JSON array

I'm looking for a way to update or better in my case concatenate each value into a JSON array. All the value are string. I know that in simpler case I could do, to replace, something like: SELECT JSON_REPLACE('[1, 2, 3]', '$[0]', 9) AS…
Derek Wildstar
  • 523
  • 1
  • 6
  • 21
1
vote
0 answers

How to filter values from JSON array in MariaDB?

I have a table which saves the score reached by some students in different schools. It has 2 columns, school_name and students (type json). The table is similar to this: school_name students Apple Pear School [ { "name": "John", "passed": 1…
AlexSp3
  • 2,201
  • 2
  • 7
  • 24
1
vote
1 answer

How to insert new value in the middle of a JSON array?

I have a JSON type field in my table, which has a value like this [1, 3] and I want to insert another value in the middle of the array, using something like a json splice function (I know it doesn't exist) /* JSON_ARRAY_SPLICE(array, start,…
AlexSp3
  • 2,201
  • 2
  • 7
  • 24
1
vote
2 answers

How to convert json MySQL data into rows and column

I have mysql table like this which contain id and json type column: id value 1 {"sys": "20", "dia": "110"} 2 {"bpm": "200"} 3 {"bpm": "123", "sys": "1", "dia": ""} Now, I want to have a MySQL query to which data should be as below in…
Aman Agrawal
  • 73
  • 1
  • 12
1
vote
0 answers

MySQL: Update specific values in JSON array of objects

I'm using MySQL 5.7.12, and have a JSON column with the following data: [{"tpe": "I", "val": 1}, {"tpe": "C", "val": 2}, {"tpe": "A", "val": 3}] I would like to UPDATE val from 2 into 20 WHERE tpe='C'. Here is my attempt: UPDATE user SET data =…
Shvalb
  • 1,835
  • 2
  • 30
  • 60
1
vote
1 answer

JSON_MERGE_PATCH with null values (in Javascript)

As written in the docs, JSON_MERGE_PATCH will remove each value that is set to null, the following example will remove the header value from my settings json field const data = JSON.stringify({header: null, otherdata: ...}) await…
Miguel Stevens
  • 8,631
  • 18
  • 66
  • 125
1
vote
1 answer

MySQL JSON Parsing - Escaped Json-within-Json

I'm having trouble using the in-built MySQL JSON functions to parse out a value from a JSON string nested inside a JSON string. Here's a sample: { "SucceededAt": "2022-01-18T07:54:50.5548083Z", "PerformanceDuration": "1463", "Latency": "91", …
GWR
  • 1,878
  • 4
  • 26
  • 44
1
vote
1 answer

Looping through an array in SQL column

I have a SQL table that looks something like this: | ID | Value | | --- | ----------------------------------------------------- | | 1 | {"name":"joe", "lastname":"doe", "age":"34"} | | 2 …
pomaaa
  • 596
  • 1
  • 4
  • 18
1
vote
1 answer

Sql constraint error references other column

I don't know a lot of mysql and have an error in my sql script. Currently running mysql 8.0.24. Does anyone know what might be the problem? Error: https://prnt.sc/226xk5x Sql: -- Dumping structure for table gtav_rp2._vehicle CREATE TABLE IF NOT…
Jens00
  • 17
  • 4
1
vote
0 answers

How to extract array values from mysql json field and return null or default if path does not exist?

I have this json in a mysql column. {"items": [ { "name": "a1", "details": {"value": 1} }, { "name": "a2", "details": {"value": 2} }, { "name": "a3" }, { "name":…
1
vote
2 answers

Extract key-pair values from JSON objects in MySQL

From MySQL JSON data field, I'm extracting data from array like so: SELECT data ->> '$.fields[*]' as fields FROM some_database... which returns: [{ "id": 111056, "hint": null, "slug": "email", "label": "E-mail", "value": null, "field_value":…
Gacek
  • 10,184
  • 9
  • 54
  • 87
1
vote
1 answer

How to query a JSON map in mysql?

I want to query a JSON mysql field as follows: json: { "key1": [ { "firstname": "jane" }, { "firstname": "john" } ] } I want to search for "key1", and extract the [0].firstname field,…
membersound
  • 81,582
  • 193
  • 585
  • 1,120