我有一个包含Json字符串记录的列。我想解析json并从select语句中获取特定键的值。
示例JSON
{"kofaxDocId":"8ae16f46-c68f-11e5-8105-0e15fb39b661","systemDocType":"Loan Application","requestId":"c770a940-b9f3-4c41-aee6-3e08c1470ec6","docType":"Loan Application","confidence":0.6499999761581421,"engineType":"kofax","completionStatus":"Suggested"}
我希望我的选择查询仅获取键“置信度”的值。我尝试使用Regex和Substring,但由于json长度不固定,因此无法为所有记录获取正确的值。
我试过这些
SELECT substring(extended_metadata, ('"confidence":', extended_metadata )) FROM documents ;
SELECT json_extract(extended_metadata,'confidence') CONFIDENCE from documents;
我的MYSQL版本不支持Json_extract()。
感谢帮助。
答案 0 :(得分:4)
MySQL在版本5.7.7 http://mysqlserverteam.com/json-labs-release-native-json-data-type-and-binary-format/
中支持JSON你必须纯粹在mysql中解析它然后我恐怕你必须将它作为一个字符串处理并从中删除它(只是正常的字符串函数或使用正则表达式)这不是优雅但它会起作用
CREATE TABLE testjson (`jsonfield` varchar(2000)) ;
INSERT INTO testjson (`jsonfield`) VALUES ('{"kofaxDocId":"8ae16f46-c68f-11e5-8105-0e15fb39b661","systemDocType":"Loan Application","requestId":"c770a940-b9f3-4c41-aee6-3e08c1470ec6","docType":"Loan Application","confidence":0.6499999761581421,"engineType":"kofax","completionStatus":"Suggested"}') ;
SELECT substring(jsonfield, locate('"confidence":',jsonfield)+13, locate(',"', jsonfield, locate('"confidence":',jsonfield))-locate('"confidence":',jsonfield)-13) as confidence_value
FROM testjson;
此查询在您的jsondata中搜索Confidence,然后在置信后查看下一个分隔符,并减去这两个索引之间的内容。