第十三章 SQL谓词 IS JSON
第十三章 SQL谓词 IS JSON
确定数据值是否为JSON格式。
注意:IRIS版本可用。其他不行。
大纲
scalar-expression IS [NOT] JSON [keyword]
参数
scalar-expression- 正在检查JSON格式的标量表达式。keyword- 可选—可选值、标量、数组或对象。
默认为VALUE。
描述
IS JSON谓词确定数据值是否为JSON格式。
下面的示例确定谓词是否是格式化正确的JSON字符串,是JSON对象还是JSON数组:
ClassMethod IsJson()
{
s q1 = "SELECT TOP 5 Name FROM Sample.Person "
s q2 = "WHERE '{""name"":""Fred"",""spouse"":""Wilma""}' IS JSON"
s myquery = q1_q2
s tStatement = ##class(%SQL.Statement).%New()
s qStatus = tStatement.%Prepare(myquery)
if qStatus'=1 {
w "%Prepare failed:"
d $System.Status.DisplayError(qStatus)
q
}
s rset = tStatement.%Execute()
d rset.%Display()
}
IS JSON(带或不带可选关键字VALUE)对任何JSON数组或JSON对象返回true。
这包括一个空JSON数组'[]'或一个空JSON对象'{}'。
关键字VALUE和关键字SCALAR是同义词。
对于JSON数组oref返回true。
对于JSON对象oref, IS JSON对象返回true。
下面的例子说明了这一点:
ClassMethod IsJson1()
{
s jarray=[1,2,3,5,8,13,21,34]
w "JSON array: ",jarray,!
s myquery = "SELECT TOP 5 Name FROM Sample.Person WHERE ? IS JSON ARRAY"
s tStatement = ##class(%SQL.Statement).%New()
s qStatus = tStatement.%Prepare(myquery)
if qStatus'=1 {
w "%Prepare failed:"
d $System.Status.DisplayError(qStatus)
q
}
s rset = tStatement.%Execute(jarray)
d rset.%Display()
}
ClassMethod IsJson2()
{
s jarray=[1,2,3,5,8,13,21,34]
w "JSON array: ",jarray,!
s myquery = "SELECT TOP 5 Name FROM Sample.Person WHERE ? IS JSON OBJECT"
s tStatement = ##class(%SQL.Statement).%New()
s qStatus = tStatement.%Prepare(myquery)
if qStatus'=1 {
w "%Prepare failed:"
d $System.Status.DisplayError(qStatus)
q
}
s rset = tStatement.%Execute(jarray)
d rset.%Display()
}
ClassMethod IsJson3()
{
s jobj={"name":"Fred","spouse":"Wilma"}
w "JSON object: ",jobj,!
s myquery = "SELECT TOP 5 Name FROM Sample.Person WHERE ? IS JSON OBJECT"
s tStatement = ##class(%SQL.Statement).%New()
s qStatus = tStatement.%Prepare(myquery)
if qStatus'=1 {
w "%Prepare failed:"
d $System.Status.DisplayError(qStatus)
q
}
s rset = tStatement.%Execute(jobj)
d rset.%Display()
}
IS NOT JSON谓词是少数几个可以在WHERE子句中用于流字段的谓词之一。
它的行为与is NOT NULL相同。
如下面的例子所示:
ClassMethod IsJson4()
{
s q1 = "SELECT Title,%OBJECT(Picture) AS PhotoOref FROM Sample.Employee "
s q2 = "WHERE Picture IS NOT JSON"
s myquery = q1_q2
s tStatement = ##class(%SQL.Statement).%New()
s qStatus = tStatement.%Prepare(myquery)
if qStatus'=1 {
w "%Prepare failed:"
d $System.Status.DisplayError(qStatus)
q
}
s rset = tStatement.%Execute()
d rset.%Display()
}
IS JSON可以在任何可以指定谓词条件的地方使用,如本手册的谓词概述页面所述。