2

How to parse Json in MySQL level? I've version MySQL version 5.6.26 in XAMPP

MySQL --version
MySQL  Ver 14.14 Distrib 5.6.26, for Win32 (x86)

I created a table.

CREATE TABLE `message` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `data` text,
  PRIMARY KEY (`id`)
);

Inserted some rows

INSERT INTO message (id,data) VALUES(1,'{"from":"chris","title":"Awesome Article","body":"Lorem ipsum dolor sit amet, consectetur adipiscing elit."}');
INSERT INTO message (id,data) VALUES(2,'{"from":"loren","title":"Another Article","body":"Lorem ipsum dolor sit amet, consectetur adipiscing elit."}');
INSERT INTO message (id,data) VALUES(3,'{"from":"jason","title":"How to run a query","body":"Lorem ipsum dolor sit amet, consectetur adipiscing elit."}');

and I try to get Json values:

SELECT Json_get(data,'title') FROM message WHERE id=2;

And I get error:

SELECT json_get(data,'title') FROM message WHERE id=2; ERROR 1305 (42000): FUNCTION mydatabasename.json_get does not exist

Has anybody ideas?

2
  • I am not very experienced with it, but I think if your data is in Json, you should use a NoSQL database, or if you use an SQL database, you better do the parsing in the business logic. It looks like you try to make your SQL database behave like a NoSQL one... Commented Dec 29, 2015 at 12:00
  • In an SQL database, your scheme would be static and your message table would contain all the fields that are in the Json data Commented Dec 29, 2015 at 12:01

3 Answers 3

2

You can parse json data using common_schema, try following code it may help you :-

SELECT common_schema.extract_json_value(data,'title') FROM message WHERE id=2;
Sign up to request clarification or add additional context in comments.

2 Comments

This suggestion doesn't work: SELECT common_schema.extract_json_value(data,'title') FROM message WHERE id=2; ERROR 1305 (42000): FUNCTION common_schema.extract_json_value does not exist
You have to install common_schema first.
0

Update your mysql to 5.7 and you can use JSON data type which is predefined by MySQL to handle your json data.

Comments

0

With the use of json_function you can do this.

SELECT JSON_EXTRACT(data,'$.title') FROM message WHERE id=1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.