如何在 MySQL 中使用 JSON_EXTRACT 并获取不带引号的字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37403039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:49:55  来源:igfitidea点击:

How can I use JSON_EXTRACT in MySQL and get a string without the quotes?

mysql

提问by Alex

If I have a simple SELECT statement like this:

如果我有一个像这样的简单 SELECT 语句:

SELECT JSON_EXTRACT('{"username":"Alexander"}', '$.username');

I would expect it to return Alexander, but instead it returns "Alexander". How can I get rid of the quotes? Why does this function even return the quotes too?

我希望它返回Alexander,但它返回"Alexander"。我怎样才能摆脱引号?为什么这个函数也返回引号?

回答by Raul

You can use JSON_UNQUOTEto achieve this.

您可以使用它JSON_UNQUOTE来实现这一点。

select JSON_UNQUOTE(JSON_EXTRACT(base, '$.scope')) as scope from t_name

ref: Functions That Modify JSON Values

参考:修改 JSON 值的函数

回答by Tharindu Kalhara

You can use SUBSTRING

您可以使用子字符串

SELECT SUBSTRING( JSON_EXTRACT ( '{"username":"Alexander"}', '$.username' ), 2, ( LENGTH( JSON_EXTRACT ( '{"username":"Alexander"}', '$.username' ) ) - 2 ) );

回答by ombre04

you can use replace() with it to remove quotation marks

您可以使用 replace() 来删除引号

SELECT replace(JSON_EXTRACT('{"username":"Alexander"}', '$.username'), '\"', '');