如何在MySql中使用布尔值创建JSON对象? [英] How to create a JSON object in MySql with a boolean value?
本文介绍了如何在MySql中使用布尔值创建JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望MySql查询返回一个如下所示的JSON对象:
I would like my MySql query to return a JSON object that looks as follows:
{"name": "Piotr", "likesMysql": true}
当我这样做时,这似乎工作得很好:
This seems to be working fine when I do:
SELECT json_object(
'name', 'Piotr',
'likesMysql', TRUE
)
但是,当我尝试从if
表达式派生likesMysql
时,我得到了
0
和1
而不是false
和true
例如:
However when I try to derive the likesMysql
from an if
expression I get
0
and 1
instead of false
and true
e.g.:
SELECT json_object(
'name', 'Piotr',
'likesMysql', if(4 MOD 2 = 0, TRUE, FALSE)
)
结果
{"name": "Piotr", "likesMysql": 1}
如何使用json_object
构造具有true
或false
作为属性值的JSON对象?
How do I use the json_object
to construct a JSON object that has true
or false
as property value?
推荐答案
这似乎是 MySql中的错误.
您可以使用cast(true as json)
解决此问题,例如:
You can workaround it though with cast(true as json)
e.g.:
SELECT json_object(
'name', 'Piotr',
'likesMysql', if(4 MOD 2 = 0, cast(TRUE as json), cast(FALSE as json))
)
这篇关于如何在MySql中使用布尔值创建JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文