I have a SQL Table X which has a JSON Column 'JsonVal' & few other Columns
Name Id Group JsonVal
----------------------------------------------------------
Name1 2 gg1 {"LogLevel":"2", "InfoLevel":"3"}
Name2 3 gg5 {"LogLevel":"4"}
After parsing and making sense of the data in JsonVal column the way i want, I want to read from table X such that i get a result like this:
Name Id Features
----------------------------------------------------------
Name1 2 {"HasLogLevel":"True", "LogLevel":"2", "HasInfoLevel":"True"}
Name2 3 {"HasLogLevel":"True", "LogLevel":"4", "HasInfoLevel":"False"}
I am not able to find a way to convert just part of my columns to JSON. If i do FOR JSON in a query like this:
SELECT Name,Id,
HasLogLevel = CASE WHEN JSON_VALUE(JsonVal,'$."LogLevel"') IS NOT NULL THEN 'True' ELSE 'False' END,
LogLevel = CASE WHEN JSON_VALUE(JsonVal,'$."LogLevel"') IS NULL THEN 'NO-VALUE' ELSE JSON_VALUE(JsonVal,'$."LogLevel"') END,
HasInfoLevel = CASE WHEN JSON_VALUE(JsonVal,'$."InfoLevel"') IS NOT NULL THEN 'True' ELSE 'False' END
FROM X FOR JSON PATH
It converts all the columns (Including Name, Id ) into JSON. But i only want the 3 columns HasLogLevel,LogLevel and HasInfoLevel to be formatted to JSON in the result
Is there a way to query and get results the way i want in SQL Server in a single query?