[英]How to convert JSON to array of strings
Is it possible to use for json path
to format rows in a JSON array format?是否可以使用for json path
以 JSON 数组格式格式化行?
I have a column like this我有这样的专栏
Col1
====
abc
def
ghi
jkl
and I want to format it like this我想像这样格式化它
{"Col1":["abc","def","ghi","jkl"]}
I have so far gotten it to look like this到目前为止,我已经让它看起来像这样
{["Col1":"abc","Col1":"def","Col1":"ghi","Col1":"jkl"]}
using this code使用此代码
select col1 from table for json path
With a bit of string manipulation.通过一些字符串操作。
Creating a simple array seems like a missed opportunity.创建一个简单的数组似乎是一个错失的机会。
Select Col1 = json_query('["'+string_agg(string_escape(Col1,'json'), '","') +'"]')
From YourTable
For json path, Without_Array_Wrapper
Results结果
{"Col1":["abc","def","ghi","jkl"]}
If your version is <2017, string_agg()
would not be available.如果您的版本是 <2017,则string_agg()
将不可用。 However, you can use the stuff()/xml
approach但是,您可以使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.