Galli
Galli

Reputation: 23

JSON JSON_EXTRACT with MySQL

I'm using the JSON_EXTRACT with MYSQL and using this command:

SET @j = '{"id" : "1"}';
SELECT JSON_EXTRACT(@j, '$.id')

the result is

"1"

but, when I use

SET @j = '[{"id" : "1"}, {"id" : "2"}]';
SELECT JSON_EXTRACT(@j, '$.id')

the result is

NULL

I expected this result

"1"
"2"

Any sugestion? I want the list of ID'S from JSON.

Upvotes: 2

Views: 1801

Answers (1)

Alejandro Landini
Alejandro Landini

Reputation: 119

try this:

SET @j = '[{"id" : "1"}, {"id" : "2"}]';
SELECT JSON_EXTRACT(@j,'$[*].id')

the result is:

["1", "2"]

Upvotes: 2

Related Questions