liberxk
liberxk

Reputation: 11

How can I convert a string to a json string with SQL Server

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

Answers (1)

Avi
Avi

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

Related Questions