- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to store data in MySQL as JSON?
We can store data in MySQL as JSON with the help of JSON data type. The following is an example.
Let us now create a table.
mysql> CREATE table JsonAsMySQLDemo -> ( -> id int, -> name varchar(100), -> PageDemo JSON, -> eventInfo JSON -> ); Query OK, 0 rows affected (0.67 sec)
Storing records into JSON data type.
mysql> INSERT into JsonAsMySQLDemo values -> ( -> 1, -> 'John', -> '{"Page1":"/"}', -> '{"OperatingSystem":"Windows"}' -> ); Query OK, 1 row affected (0.24 sec) mysql> insert into JsonAsMySQLDemo values -> ( -> 2, -> 'Bob', -> '{"Page2":"/"}', -> '{"OperatingSystem":"Solaris"}' -> ); Query OK, 1 row affected (0.14 sec)
To display all records, use the SELECT
mysql> select *from JsonAsMySQLDemo;
The following is the output.
+------+------+----------------+--------------------------------+ | id | name | PageDemo | eventInfo | +------+------+----------------+--------------------------------+ | 1 | John | {"Page1": "/"} | {"OperatingSystem": "Windows"} | | 2 | Bob | {"Page2": "/"} | {"OperatingSystem": "Solaris"} | +------+------+----------------+--------------------------------+ 2 rows in set (0.00 sec)
Advertisements