DECLARE @Shopping xml;
SET @Shopping = (SELECT ExpenseDate, ExpenseAmount, c.CategoryName, ExpenseDesc
FROM SB_ExpenseItems e
JOIN SB_ExpenseCategories c on (c.CategoryID = e.CategoryID)
FOR XML path, root)
--Ref: https://www.codeproject.com/Articles/815371/Data-Parsing-SQL-to-JSON
-- Function for Conversion | XML to JSON
SELECT Stuff(
(SELECT * from
(SELECT ',
{'+
Stuff((SELECT ',"'+coalesce(b.c.value('local-name(.)', 'NVARCHAR(MAX)'),'')+'":"'+
b.c.value('text()[1]','NVARCHAR(MAX)') +'"'
from x.a.nodes('*') b(c)
for xml path(''),TYPE).value('(./text())[1]','NVARCHAR(MAX)')
,1,1,'')+'}'
from @Shopping.nodes('/root/*') x(a)
) JSON(theLine)
for xml path(''),TYPE).value('.','NVARCHAR(MAX)' )
,1,1,'')
SET @Shopping = (SELECT ExpenseDate, ExpenseAmount, c.CategoryName, ExpenseDesc
FROM SB_ExpenseItems e
JOIN SB_ExpenseCategories c on (c.CategoryID = e.CategoryID)
FOR XML path, root)
--Ref: https://www.codeproject.com/Articles/815371/Data-Parsing-SQL-to-JSON
-- Function for Conversion | XML to JSON
SELECT Stuff(
(SELECT * from
(SELECT ',
{'+
Stuff((SELECT ',"'+coalesce(b.c.value('local-name(.)', 'NVARCHAR(MAX)'),'')+'":"'+
b.c.value('text()[1]','NVARCHAR(MAX)') +'"'
from x.a.nodes('*') b(c)
for xml path(''),TYPE).value('(./text())[1]','NVARCHAR(MAX)')
,1,1,'')+'}'
from @Shopping.nodes('/root/*') x(a)
) JSON(theLine)
for xml path(''),TYPE).value('.','NVARCHAR(MAX)' )
,1,1,'')
0 nhận xét:
Đăng nhận xét