Reputation: 11
Example: now my string is
aa, bb, cc
How can I convert it to
[{"name","aa"},{"name":"bb"},{"name":"cc"}]
with SQL Server
Upvotes: 1
Views: 750
Reputation: 1845
One way to achieve this is using String split and JSON path function.
create table jsonprac
(name varchar(100) )
insert into jsonprac values ('aa,bb,cc')
select
(select s.value as [name] from jsonprac p
cross apply string_split(p.name,',') s
for json path, INCLUDE_NULL_VALUES ) JsonValue
Output:
JsonValue
[{"name":"aa"},{"name":"bb"},{"name":"cc"}]
Upvotes: 2