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

I want to retrieve SUM using key of JSON field in MySQL

I want to get the summation of vehicle_rent, material_amount, other_amount, labour_amount, pa_amount and ta_amount from JSON type data field using MySQL json_extract and json_object or any other json related methods of MySQL. { "task_vehicle":…
0
votes
1 answer

How do we modify an json array object regardless of its position?

The problem Each entity owns an id and a json field. That json field simply stores a json list of objects. Entity{ id, json } "1, '[{"tag": "Player"}, {"position": {"x": 20, "y": 20}}]'" The order of those json objects is not always the same and i…
genaray
  • 1,080
  • 1
  • 9
  • 30
0
votes
0 answers

JSON_EXTRACT() in MariaDB trigger says error in json, JSON_VALID() says its valid

I have a very strange issue going on with a trigger I'm trying to create. I get the error Syntax error in JSON text in argument 1 to function 'json_extract' at position 13 The trigger is as follows CREATE DEFINER=`DBuser`@`%` TRIGGER…
0
votes
0 answers

Parse JSON And Insert Into MySQL

I am receiving JSON data to my server from each client. I have three main tables; datatypes, templaricustomers and mqttpacket. Here the datatypes are coming from JSON variable names and I am keeping them in the database. As I am a beginner in MySQL,…
0
votes
0 answers

AWS Athena query to get only one object in array

My data looks like this (table name: test_table): { timestamp: 'some_timestamp', headers: [{'name': 'test', 'value': 'sth'}, {'name': 'test2', 'value': 'sth2'}, {'name': 'test3', 'value': 'sth3'}] } How can I select only {'name': 'test2',…
Murakami
  • 3,474
  • 7
  • 35
  • 89
0
votes
2 answers

MariaDB parse a JSON String

This works in SQL Server: declare @jstring varchar(max)='{ "FieldInfo": { "Template": "Babies", "Groups": [{ "Group": "Required", "SortOrder": 1, "BackgroundColor": "FCD5B4", …
Chris
  • 650
  • 7
  • 20
0
votes
1 answer

How to escape a period in mySql JSON query

I am working on a project where i am required to store data in JSON format using mySQL DB. I want to store data in this format: { email1:{ data:"This is some data of email1", status:"registered", count:100 }, email1:{ data:"This is some data of…
Kartik Gautam
  • 25
  • 1
  • 5
0
votes
1 answer

Query of json arrays containing objects

It is stored in MySQL DB in the form below. "config" is a JSON type and it's an array of objects. I want to query the data that has "config" containing the object "type" is "download". (i.e. data with "id=test2" and "id=test3" in the example…
justin
  • 13
  • 1
  • 3
0
votes
1 answer

How to generate JSON with nested elements from MySql

I have table like this: +----+-------+-------+--------------+ | id | title | city | street | +----+-------+-------+--------------+ | 1 | First | London| Oxford | +----+-------+-------+--------------+ | 2 | Second| Berlin|…
Ramis
  • 13,985
  • 7
  • 81
  • 100
0
votes
1 answer

Retrieve property values from json array filtered on another property value

I have a table with a column (column_metadata) that holds a json array [{ "ORDER": -1, "READONLY": false, "COLUMNNAME": "id", "COLUMNTYPE": "int", "FILTERABLE": false, "SUMMARYVIEW": false, "WHITELISTED": false }, { …
lohiarahul
  • 1,432
  • 3
  • 22
  • 35
0
votes
1 answer

I need help finding wildcard to use in mysql funcion JSON_SEARCH

I'm tring to get the right wildcard (trying [0-9]%) using: JSON_SEARCH(packages, 'one', 'openstack-neutron-[0-9]%', null, '$.packages[*]') I just need to get "openstack-neutron-12.1.0-2-el7ost.noarch" but not all…
davser
  • 85
  • 1
  • 1
  • 9
0
votes
1 answer

How to get MYSQL JSON object value when don't know the key?

I have a column in MYSQL table from which I want to get object's first value, and I don't know what is the kay name. Values are like: {"5":"ABC"} {"8":"CDE"} I have tried JSON_EXTRACT, JSON_KEYS but nothing work for me.
Anand agrawal
  • 492
  • 6
  • 25
0
votes
0 answers

Fetch matched object from JSON column with an array of object in MySQL

I have a json column with the following array: [ { "name":"Raj", "email":"raj@gmail.com", "age":32 }, { "name":"Mohan", "email":"Mohan@yahoo.com", "age":21 } ] I want to get an object…
5a01d01P
  • 663
  • 2
  • 9
  • 20
0
votes
0 answers

Mysql query to search data into Mysql if column contain multidimensional array in JSON form?

I have a product table under which i have create field name : product_attributes under which I have stored all product attributes in json data as show…
0
votes
0 answers

mysql select query WHERE json_contains matches but does not return data?

Context: I'm tying to finish up a project that uses paypal subscriptions. The front end, with the users authenticated cookie in place, saves the subscription id to the company the user belongs to when they agree to the subscription. Then, when the…
John
  • 976
  • 1
  • 15
  • 21