Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON_EXTRACT not working when not all rows contain JSON

Tags:

mysql

I am trying to run JSON EXTRACT but get the following error:

Data truncation: Invalid JSON text in argument 1 to function json_extract: "The document is empty." at position 0.

select id, JSON_EXTRACT(content, "$.pathway_id") from reports
like image 843
ykay says Reinstate Monica Avatar asked Aug 30 '18 12:08

ykay says Reinstate Monica


1 Answers

You can prevent this error by using JSON_VALID to make sure the field contains JSON as follows:

select id, CASE WHEN JSON_VALID(content) THEN JSON_EXTRACT(content, "$.pathway_id") ELSE null END from reports
like image 125
ykay says Reinstate Monica Avatar answered Oct 16 '22 16:10

ykay says Reinstate Monica