MOCKSTACKS
EN
Questions And Answers

More Tutorials









MYSQL JSON

Create simple table with a primary key and JSON field


CREATE TABLE table_name (
 id INT NOT NULL AUTO_INCREMENT,
 json_col JSON,
 PRIMARY KEY(id)
);

Insert a simple JSON


INSERT INTO
 table_name (json_col)
VALUES
 ('{"City": "Galle", "Description": "Best damn city in the world"}');

That's simple as it can get but note that because JSON dictionary keys have to be surrounded by double quotes the entire thing should be wrapped in single quotes. If the query succeeds, the data will be stored in a binary format.

Updating a JSON field


In the previous example we saw how mixed data types can be inserted into a JSON field. What if we want to update that field? We are going to add scheveningen to the array named variations in the previous example.

UPDATE
 myjson
SET
 dict=JSON_ARRAY_APPEND(dict,'$.variations','scheveningen')
WHERE
 id = 2;

Notes:


1. The $.variations array in our json dictionary. The $ symbol represents the json documentation. For a full
explaination of json paths

2. Since we don't yet have an example on querying using json fields, this example uses the primary key.

Now if we do SELECT * FROM myjson we will see

+----+-----------------------------------------------------------------------------------------+
| id | dict |
+---+-----------------------------------------------------------------------------------------+
| 2 | {"opening": "Sicilian", "variations": ["pelikan", "dragon", "najdorf", "scheveningen"]} |
+----+-----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


Conclusion

In this page (written and validated by ) you learned about MYSQL JSON . What's Next? If you are interested in completing MYSQL tutorial, your next topic will be learning about: MYSQL Extract values from JSON type.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.