如何将 JSON 转换为字符串数组
How to convert JSON to array of strings
是否可以使用 for json path
将行格式化为 JSON 数组格式?
我有一个这样的专栏
Col1
====
abc
def
ghi
jkl
我想这样格式化
{"Col1":["abc","def","ghi","jkl"]}
到目前为止我已经把它变成了这个样子
{["Col1":"abc","Col1":"def","Col1":"ghi","Col1":"jkl"]}
使用此代码
select col1 from table for json path
对字符串进行一些操作。
创建一个简单的数组似乎错失了机会。
Select Col1 = json_query('["'+string_agg(string_escape(Col1,'json'), '","') +'"]')
From YourTable
For json path, Without_Array_Wrapper
结果
{"Col1":["abc","def","ghi","jkl"]}
如果您的版本是 <2017,string_agg()
将不可用。但是,您可以使用 stuff()/xml
方法
Select Col1 = json_query('["'+stuff((Select concat('","',string_escape(Col1,'json'))
From YourTable
For XML Path ('')),1,3,'')+'"]')
For json path, Without_Array_Wrapper
是否可以使用 for json path
将行格式化为 JSON 数组格式?
我有一个这样的专栏
Col1
====
abc
def
ghi
jkl
我想这样格式化
{"Col1":["abc","def","ghi","jkl"]}
到目前为止我已经把它变成了这个样子
{["Col1":"abc","Col1":"def","Col1":"ghi","Col1":"jkl"]}
使用此代码
select col1 from table for json path
对字符串进行一些操作。
创建一个简单的数组似乎错失了机会。
Select Col1 = json_query('["'+string_agg(string_escape(Col1,'json'), '","') +'"]')
From YourTable
For json path, Without_Array_Wrapper
结果
{"Col1":["abc","def","ghi","jkl"]}
如果您的版本是 <2017,string_agg()
将不可用。但是,您可以使用 stuff()/xml
方法
Select Col1 = json_query('["'+stuff((Select concat('","',string_escape(Col1,'json'))
From YourTable
For XML Path ('')),1,3,'')+'"]')
For json path, Without_Array_Wrapper