關(guān)于mysql中的json解析函數(shù)JSON_EXTRACT
mysql json解析函數(shù)JSON_EXTRACT
MYSQl自帶的解析函數(shù)JSON_EXTRACT,用JSON_EXTRACT函數(shù)解析出來的函數(shù)會包含雙引號
例子
t_table表里面source字段是以json格式寫的值為
{ "info" : { "color" : "白色", "inner_color" : "米色", "number" : "12345678", "registration_date" : "2012-11" },
"accessory" : [ "1", "4", "5", "6", "7", "8", "9", "10" ],
"remark" : "測試"}查詢color不去掉雙引號,inner_color去掉雙引號,remark去掉雙引
select? source->'$.info.color' ?as color, replace(source->'$.info.inner_color','"','') ? as inner_color, replace(source->'$.remark','"','') ? as remark ?from t_table
查詢結(jié)果
| color | inner_color | remark |
|---|---|---|
| “白色” | 米色 | 測試 |
mysql5.7 json格式與json_extract方法
數(shù)據(jù)初始化
json_test表數(shù)據(jù),id和jsonstr字段(類型json)
{
"no": "7",
"title": "運動方式",
"content": [{
"text": "您平時經(jīng)常進行的運動及時間",
"item1": [{
"text": "慢跑 / 快走 / 走跑結(jié)合",
"type": "select",
"value": "selected"
}, {
"text": "上下樓梯 / 爬山",
"type": "multselect",
"value": "selected"
}],
"item2": [{
"text": "慢跑222走跑結(jié)合",
"type": "text",
"value": "慢跑2"
}, {
"text": "上下樓梯 / 爬山2",
"type": "number",
"value": "33"
}]
}]
}select語句
SELECT ? ? id, ? ? json_extract( t.jsonstr, '$.*' ), ? ? json_extract( t.jsonstr, '$.title' ) AS "title", ? ? json_extract( t.jsonstr, '$.content' ) AS "content" , ? ? json_extract( t.jsonstr, '$**.text' ) AS "text" , ? ? json_extract( t.jsonstr, '$.content[*].item1[*]' ) AS "item1"? FROM ? ? json_test t;
返回結(jié)果解析
//json_extract( t.jsonstr, '$.*' )返回:
["7", "運動方式", [{"text": "您平時經(jīng)常進行的運動及時間", "item1": [{"text": "慢跑 / 快走 / 走跑結(jié)合", "type": "select", "value": "selected"}, {"text": "上下樓梯 / 爬山", "type": "multselect", "value": "selected"}], "item2": [{"text": "慢跑222走跑結(jié)合", "type": "text", "value": "慢跑2"}, {"text": "上下樓梯 / 爬山2", "type": "number", "value": "33"}]}]]
//json_extract( t.jsonstr, '$.title' ) AS "title"返回:
"運動方式"
//json_extract( t.jsonstr, '$.content' ) AS "content" 返回:
[{"text": "您平時經(jīng)常進行的運動及時間", "item1": [{"text": "慢跑 / 快走 / 走跑結(jié)合", "type": "select", "value": "selected"}, {"text": "上下樓梯 / 爬山", "type": "multselect", "value": "selected"}], "item2": [{"text": "慢跑222走跑結(jié)合", "type": "text", "value": "慢跑2"}, {"text": "上下樓梯 / 爬山2", "type": "number", "value": "33"}]}]
//json_extract( t.jsonstr, '$**.text' ) AS "text" 返回:
["您平時經(jīng)常進行的運動及時間", "慢跑 / 快走 / 走跑結(jié)合", "上下樓梯 / 爬山", "慢跑222走跑結(jié)合", "上下樓梯 / 爬山2"]
//json_extract( t.jsonstr, '$.content[*].item1[*]' ) AS "item1" 返回:
[{"text": "慢跑 / 快走 / 走跑結(jié)合", "type": "select", "value": "selected"}, {"text": "上下樓梯 / 爬山", "type": "multselect", "value": "selected"}]用法解析
| ‘$.*’ | 返回全部json |
| ‘$.title’ | 返回key=”title”的數(shù)據(jù) |
| ‘$**.text’ | 返回所有最底層key=”text”的數(shù)據(jù) |
| ‘$.content[*].item1[*]’ | 返回key=content的list的key=item1的list的所有內(nèi)容 |
官方文檔:https://dev.mysql.com/doc/refman/5.7/en/json.html
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
textarea標簽(存取數(shù)據(jù)庫mysql)的換行方法
textarea標簽本身不識別換行功能,回車換行用的是\n換行符,輸入時的確有換行的效果,但是html渲染或者保存數(shù)據(jù)庫mysql時就只是一個空格了,這時就需要利用換行符\n和br標簽的轉(zhuǎn)換進行處理2023-09-09
mysql中一個普通ERROR 1135 (HY000)錯誤引發(fā)的血案
ERROR 1135 (HY000): Can’t create a new thread (errno 11);if you are not out of available memory,you can consult the manual for a possible OS-dependent bug2015-08-08
MySql 快速插入千萬級大數(shù)據(jù)的方法示例
這篇文章主要介紹了MySql 快速插入千萬級大數(shù)據(jù)的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08

