Link to home
Start Free TrialLog in
Avatar of Allen Hujsak
Allen Hujsak

asked on

MySQL 5.7 JSON data type. 3 part question about proper query formatting. Inserting, updating by adding to an array or object

MySQL 5.7 and the JSON data type, Help with basic 3 basic functions / queries


I have a data structure I am trying to work with in experimenting and doing a proof of concept using the JSON data type in MySQL. I am using node.js for my back end.

Here is my table syntax
DROP TABLE if exists user;
CREATE TABLE user (
  `id` varchar(128) NOT NULL,
	`device_id` varchar(128) NOT NULL,
  `email` varchar(100) DEFAULT NULL,
	`device_type` enum('','android','ios','web') DEFAULT NULL,
  `password` varchar(512) DEFAULT NULL,
  `login_by` enum('manual','facebook','anon') DEFAULT 'anon',
  `created_timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified_timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `data` json NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `device_id` (`device_id`),
  UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Open in new window


This what I want my JSON data object to look like
{
  "name": "Allen",
  "albums": []
}

Open in new window

Issue 1.

When I create a user this is the query being executed. It is formatted as it is by node to actually run. This is based on the documentation I have seen

insert into user (id, device_id, device_type, data ) values ("39c48d40-de07-11e8-9d18-518c0c9d3ee2","this4","ios",'{"name": "allen", "albums": []}')

Open in new window

This works however creates a string in the JSON data type. I thought the data type was supposed to be binary
If I do a
select * from user 

Open in new window

then my results will return the JSON object as a string I need to parse.
{
        "id": "39c48d40-de07-11e8-9d18-518c0c9d3ee9",
        "device_id": "this3",
        "email": null,
        "device_type": "ios",
        "password": null,
        "login_by": "anon",
        "created_timestamp": "2018-11-01 14:52:05",
        "modified_timestamp": "2018-11-01 14:52:05",
        "data": "{\"name\": \"Allen\", \"albums\": []}"
    }

Open in new window


If I try to put in straight JSON Object like this I get a SQL error.
insert into user (id, device_id, device_type, data ) values ("39c48d40-de07-11e8-9d18-518c0c9d3ee9","this4","ios",{"name": "allen", "likes": []})

Open in new window

Question
How do I create the JSON data type that is binary and not a serialized string.

Issue 2

This is related to issue one.
I am hoping to add an object to the array for albums
This query worked but with issues
UPDATE user SET data = JSON_ARRAY_APPEND( `data` , '$.albums' , '{"id":"sd123asdwe","title":"OU812"}' ) WHERE id = "39c48d40-de07-11e8-9d18-518c0c9d3ee9"

Open in new window


It added an additional serialized string to the overall object, so now when I get my data I need to parse the json, then loop through the array and parse the individual items..
{
        "id": "5c4c5490-dddc-11e8-b639-2fb5d5a2214b",
        "device_id": "this2",
        "email": null,
        "device_type": "ios",
        "password": null,
        "login_by": "anon",
        "created_timestamp": "2018-11-01 09:45:14",
        "modified_timestamp": "2018-11-01 09:45:14",
        "data": "{\"likes\": [\"{\\\"id\\\":\\\"sd123asdwe\\\",\\\"title\\\":\\\"OU812\\\"}\"]}"
    }

Open in new window

There has to be a better way.

Question 1.
What is the syntax to insert a native JSON object into the array? I want to avoid having to loop to deserialize.

After I added several albums I wanted a function to remove one of them. I was able to successfully remove using

UPDATE user SET data = JSON_REMOVE( `data` , '$.albums[0]' ) WHERE id = "39c48d40-de07-11e8-9d18-518c0c9d3ee9"

Open in new window

Question 2.

What would the query look like if albums was an object and I wanted to add an object to it, for example

{
  "OU812": {
    "title": "OU812",
    "artist": "Van Halen"
  }
}

Open in new window


Question 3

If I add to the object as in question 2, how would I delete a specific key?

I will have more questions. I am looking for specific code examples based on what I have put here
Avatar of David Favor
David Favor
Flag of United States of America image

1) How do I create the JSON data type that is binary and not a serialized string.

You can't as JSON is by definition built of strings. You can load binary into a string + safest way to do this is via an encode first as Base64.

https://stackoverflow.com/questions/1443158/binary-data-in-json-string-something-better-than-base64 provides a good discussion about the topic of injecting binary data into JSON objects.

2) What would the query look like if albums was an object and I wanted to add an object to it, for example

https://www.sitepoint.com/use-json-data-fields-mysql-databases/ provides a good overview of both inserting + selection JSON data.

Rather than repeat their long coverage, scan their discussion.

3) If I add to the object as in question 2, how would I delete a specific key?

You'd use the same exact SELECT format, only formatted as a DELETE statement, just like any SQL DELETE.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.