1

Example: now my string is

aa, bb, cc

How can I convert it to

[{"name","aa"},{"name":"bb"},{"name":"cc"}]

with SQL Server

1
  • Either split the string and use for json or just manually alter the string with replace. Commented Nov 29, 2018 at 3:13

1 Answer 1

2

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"}]
Sign up to request clarification or add additional context in comments.

1 Comment

Of course, this only works in SQL Server 2016 or newer which has the for json path clause

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.