Using SQL Server 2022, I'm trying to figure out how to use STRING_SPLIT
in a set-based way to convert a table column where all rows have a variable number of key/value pairs into a table without duplicates, similar to T-SQL split string but not quite.
create table #mashed (mashedtext nvarchar(1600))
insert into #mashed
values ('"id":"one", "code":"aaa", "dev":"yes"')
insert into #mashed
values ('"id":"two", "code":"bbb", "dev":"yes"')
insert into #mashed
values ('"id":"three", "code":"ccc", "dev":"no"')
insert into #mashed
values ('"id":"three", "code":"bbb", "help":"no" , "rid":"6"')
Desired shape of output
key value
----------------
id one
id two
id three
code aaa
code bbb
code ccc
dev yes
dev no
help no
rid 6
This is clearly wrong:
SELECT value
FROM STRING_SPLIT (SELECT mashedtext FROM #mashed, ',')
So what is the right way to do this without RBAR? I am sure there are two splits needed, once to get the pairs and again on each pair.
Thanks.