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

How to remove quotes from ARRAY_AGG in mysql using knex

I am using knex and trying to query for an array of objects using JSON_ARRAYAGG and JSON_OBJECT. Unfortunately, the whole array that is being returned from JSON_ARRAYAGG is wrapped in single quotes, and I can't seem to get rid of them. My query…
Josee
  • 141
  • 1
  • 2
  • 12
0
votes
1 answer

Find all records by the value of the json key in MariaDB 10.1

I have MariaDB 10.1. - I can't use JSON functions - JSON_EXTRACT etc.). In the database I have a table CONTRACTS and a column data, which contains JSON (data type TEXT): {"879": "Test", "880": "15255", "881": "2021-10-22"} And I need to find all…
0
votes
2 answers

Join all JSON object string values

In a MySQL 8 JSON column I have a JSON object with values of different types (but with no nested objects). Like this: { "abc": "Something123", "foo": 63.4, "bar": "Hi world!", "xyz": false } What is the simplest way to select the…
at54321
  • 8,726
  • 26
  • 46
0
votes
1 answer

How to return a mysql JSON array of GROUP_CONCAT output?

I have a table mapping departments and teams in MySQL. I want to retrieve array of teams for each department. For example, if I have two departments depA (teams teamAA, teamAB), depB (teams teamBA, teamBB, teamBC), I want to obtain following JSON [ …
Uttam
  • 576
  • 5
  • 17
0
votes
0 answers

How to use Trim in sql with FOR JSON

I want so select a column and wants to trim it with FOR JSON. SELECT LOWER(LTRIM(RTRIM(column_name))) AS a FROM table_name FOR JSON will TRIM work along with FOR JSON ? If not what is alternative for it?.
0
votes
1 answer

How do I change an array-valued key of a json field in a MYSQL database using JSON_REPLACE and JSON_ARRAY?

I'm trying to change one of the array-valued keys (called religions) of a JSON field named preferences in a MySQL table. I'm not using JSON_ARRAY_APPEND because the changes in the field can be arbitrary and can involve both removals and…
Jay
  • 147
  • 3
  • 11
0
votes
1 answer

Transform JSON field with MySQL

I have a JSON field in a tableA in which I save the moment when a row changes from one state to another like so, row_id state_history 1 {"2021-09-14 21:00": "State #4", "2021-09-16 21:00": "State #1", "2021-09-17 21:00": "State…
J Czerjgae
  • 5
  • 1
  • 3
0
votes
1 answer

Using the key from JSON in query

I'm new to MySQL and received a task which requires a complex(for me) query. I read the documentation and a few sources but I still cannot write it myself. I'm selecting a rows from a table where in one of the cells I have JSON like this one { …
krisgiyan
  • 7
  • 1
  • 5
0
votes
0 answers

Replace all values in a Key-value pair in JSON document in MySql

Deal all, I have a JSON data as follows: set @injsondata = '{ "action": "SaveEventByMinutes", "eventbyminutes": [{"matchid":10001,"minute":"1","halftime":"1","value":"A","userid":1,"createddate":"2021-09-25"}, …
0
votes
1 answer

Split JSON "dictonary" into separate rows

colleagues. I have a table like this: id param1 param2 config 123 456 789 {"confToUse": "b", "configs": {"a": { "qwe": "rty" }, "b": { "asd", "fgh" } } } Small explanation: configurations live in a json 'dictionary' property configs,…
Ivan Khorin
  • 827
  • 1
  • 5
  • 17
0
votes
1 answer

Inserting into a strings with special characters into an json mysql column (using php/mysql/Laravel)

I am performing an insert on db table that has a column called json_props - when a word that has special characters like Everyone's this appears in the json column (and back on the frontend) like this {"col1": "Everyone's"} I am using the…
Zabs
  • 13,852
  • 45
  • 173
  • 297
0
votes
1 answer

How to search by exact hash in Rails ActiveRecord for JSON MySql field?

I have a field with json type in MySQL database. The field contains values like { "city_eq": "NewYork", "rent_true": false, "estate_type_eq": 0 } What is the proper way to search records by incoming hash? Like…
Meliborn
  • 6,495
  • 6
  • 34
  • 53
0
votes
2 answers

SQL json_extract returns null

I am attempting to extract from my json object hits = [{“title”: “Facebook”, “domain”: “facebook.com”}, {“title”: “Linkedin”, “domain”: “linkedin.com”}] When I use: json_extract(hits,'$.title') as title, nothing is…
0
votes
1 answer

Creating JSON objects from JSON object properties in MySQL query

I need to create a list of JSON objects from properties in JSON object using MySQL 5.7. I have this structure: { "X": 1, "Y": 1, "Z": 55, "A": 2, "B": 33 } I want to have the properties to be separated as objects and sorted by…
Borys Zielonka
  • 287
  • 2
  • 6
  • 20
0
votes
1 answer

How can I update json column with a correct data type with PDO?

I have MySQL 5.7 and a query with PDO: $data['id'] = 1; $data['key'] = 'new_field'; $data['value'] = 1; $query= " UPDATE `table` SET `data` = JSON_MERGE_PATCH(`data`, JSON_OBJECT(:key, :value)) …