user2301515
user2301515

Reputation: 5117

Parsing json with mysql

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?

Upvotes: 2

Views: 14241

Answers (3)

Mox
Mox

Reputation: 61

With the use of json_function you can do this.

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

Upvotes: 0

Ahmed Khan
Ahmed Khan

Reputation: 516

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

Upvotes: 0

Harsh Sanghani
Harsh Sanghani

Reputation: 1722

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;

Upvotes: 2

Related Questions