In many cases, we need to parse the json string directly in sql. Here is a distinction based on the watershed of mysql5.7 version.
Check the MySQL version:
SELECT VERSION();
For mysql5.7 and above
Use MySQL's built-in function JSON_EXTRACT(column,'$.key'). This function has two parameters. The first parameter column represents the column name of the json column; the second parameter key represents a key in the json string.
SELECT JSON_EXTRACT('{"name":"张三","age":"18"}', '$.name') AS '姓名';
输出结果:"张三"
From the above results, there are more quotation marks in the obtained output result, so you need to consider introducing the JSON_UNQUOTE function, which is used to remove the quotation marks (" ") outside the obtained content
SELECT JSON_UNQUOTE(JSON_EXTRACT('{"name":"张三","age":"18"}', '$.name')) AS '姓名';
输出结果:张三
The above is a regular json string format. If a jsonArray is stored in a certain column, what should I do at this time? Then look down:
Examples: