返回包含数组的Json的SQL查询
我有一个包含普通数据的关系数据库(SQL Server) 我正在尝试用这种格式构建一些东西:返回包含数组的Json的SQL查询,json,sql-server,formatting,Json,Sql Server,Formatting,我有一个包含普通数据的关系数据库(SQL Server) 我正在尝试用这种格式构建一些东西: { "type": "FeatureCollection", "features": [ { "type": "Feature", "id": "1", "properties": { "address": "2" }, "geometry": { "type": "Point",
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": "1",
"properties": { "address": "2" },
"geometry": {
"type": "Point",
"coordinates": [36.33456, 59.523456]
}
},
{
"type": "Feature",
"id": "2",
"properties": { "address": "151" },
"geometry": {
"type": "Point",
"coordinates": [36.33456, 59.523456]
}
}]
}
到目前为止,我编写了以下查询:
select top 10
'Feature' as [type],
m.Id as id, m.Address as 'properties.address',
'Point' as 'geometry.type',
'[' + m.location + ']' as 'geometry.coordinates'
from
Buildings m
where
m.Location is not null
and m.Location <> ''
for json path, root('features')
“type”:“FeatureCollection”
没有测试数据是很困难的,但我认为您可以使用以下语句构建预期的JSöN输出。您还需要为JSON PATH(生成外部JSON对象)再调用一次
,并调用JSON\u查询
(返回标量值的JSON数组,而不是保存数组的文本):
表:
CREATE TABLE Buildings (
Id int,
Address varchar(100),
Location varchar(100)
)
INSERT INTO Buildings (Id, Address, Location)
VALUES
(250343, 'there', '5714843008,3363769468.235179'),
(266306, 'here', '36.38449104993326,59.48238372802735')
声明:
SELECT
[type] = 'FeatureCollection',
[features] = JSON_QUERY((
select top 10
'Feature' as [type],
m.Id as id, m.Address as 'properties.address',
'Point' as 'geometry.type',
JSON_QUERY('[' + m.location + ']') as 'geometry.coordinates'
from
Buildings m
where
m.Location is not null
and m.Location <> ''
for json path
))
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
我找到了一种使用STUFF(MySQL中的GROUP_CONCAT)和XML构建自定义JSON的方法。
看看这是否适合您:
1问题1的答案-注意,这是一个两步过程:
- a) 用于保存查询数据的变量
- DECLARE@QUERY\u DATA varchar(8000)
- b) 用于获取内部JSON元数据格式的查询数据的自定义查询(全部在一行中)
- 设置@QUERY\u数据=(
- 挑选材料(
- (
- 选择“,”+CONCAT(“{”类型“:”特征“,”id“,”m.id“,”属性“:{”地址“,”m.address“,”,”几何“,{”类型“:”点“,”坐标“:[”,m.location“,]}”
- )
- 从
- 建筑物m
- 在哪里
- m、 位置不为空
- 和m.地点“
- 对于XML路径(“”),键入.value(“”、'NVARCHAR(MAX)')、1、1、“”)
+)
- c) 最终输出-连接/加入JSON
- 选择CONCAT(“{”类型“:”特征集合“,”,“,”特征“:[”,@QUERY_DATA,]}”)
2-使用分离的X和Y坐标作为两个整数类型元素的统一解决方案回答问题2
{'X':',强制转换(左(m.location,CHARINDEX(',','m.location)-1)为int),','Y':',强制转换(右(m.location,LEN(m.location)-CHARINDEX(',','m.location))为int),'
因此,当我们将其添加到一起时,您的新查询将如下所示:
- a) 用于保存查询数据的变量
+DECLARE@QUERY\u DATA varchar(8000)
- b) 自定义查询,获取内部JSON元数据格式的查询数据(全部在一行中),包括将坐标拆分为两个整数类型元素
- 设置@QUERY\u数据=(
- 挑选材料(
- (
- 选择“,”+CONCAT(“{”类型“:”特征“,”id“,”m.id“,”属性“:{”地址“,”几何“,”{”类型“:”点“,”坐标“:[{”X“:”,强制转换(左(m.location,CHARINDEX(“,”,m.location)-1作为int)、“,”Y“:”,强制转换(右(m.location,LEN(m.location)-CHARINDEX(“,”m.location))作为int),”}
- )
- 从
- 建筑物m
- 在哪里
- m、 位置不为空
- 和m.地点“
- 对于XML路径(“”),键入.value(“”、'NVARCHAR(MAX)')、1、1、“”)
+)
- c) 最终输出-连接/连接外部和内部JSON
- 选择CONCAT(“{”类型“:”特征集合“,”,“,”特征“:[”,@QUERY_DATA,]}”)
我在提问之前已经测试过了,如果我没记错的话,我得到了一个错误,比如“关键字选择附近的语法不正确…”现在要么不可能,要么语法不正确。
SELECT
[type] = 'FeatureCollection',
[features] = JSON_QUERY((
select top 10
'Feature' as [type],
m.Id as id, m.Address as 'properties.address',
'Point' as 'geometry.type',
JSON_QUERY('[' + m.location + ']') as 'geometry.coordinates'
from
Buildings m
where
m.Location is not null
and m.Location <> ''
for json path
))
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
{
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"id":250343,
"properties":{
"address":"there"
},
"geometry":{
"type":"Point",
"coordinates":[
5714843008,
3363769468.235179
]
}
},
{
"type":"Feature",
"id":266306,
"properties":{
"address":"here"
},
"geometry":{
"type":"Point",
"coordinates":[
36.38449104993326,
59.48238372802735
]
}
}
]
}