Gaurang s
Gaurang s

Reputation: 842

Get Nested JSON from SQL table

Below is my table data looks like.

enter image description here

Below is SQL Query for Create table

CREATE TABLE [dbo].[CategoryMaster](
    [CategoryId] [int] NOT NULL,
    [ParentId] [int] NULL,
    [Name] [varchar](50) NULL,
 CONSTRAINT [PK_CategoryMaster] PRIMARY KEY CLUSTERED 
(
    [CategoryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT [dbo].[CategoryMaster] ([CategoryId], [ParentId], [Name]) VALUES (1, NULL, N'Toys & Games')
GO
INSERT [dbo].[CategoryMaster] ([CategoryId], [ParentId], [Name]) VALUES (2, 1, N'Art And Crafts')
GO
INSERT [dbo].[CategoryMaster] ([CategoryId], [ParentId], [Name]) VALUES (3, 1, N'Baby & Toddler Toys')
GO
INSERT [dbo].[CategoryMaster] ([CategoryId], [ParentId], [Name]) VALUES (4, 1, N'Bikes, Trikes & Ride-Ons')
GO
INSERT [dbo].[CategoryMaster] ([CategoryId], [ParentId], [Name]) VALUES (5, 2, N'Aprons & Smocks')
GO
INSERT [dbo].[CategoryMaster] ([CategoryId], [ParentId], [Name]) VALUES (6, 2, N'Blackboards & Whiteboards')
GO
INSERT [dbo].[CategoryMaster] ([CategoryId], [ParentId], [Name]) VALUES (7, 2, N'Clay & Dough')
GO
INSERT [dbo].[CategoryMaster] ([CategoryId], [ParentId], [Name]) VALUES (8, 1, N'Pretend Play')
GO
INSERT [dbo].[CategoryMaster] ([CategoryId], [ParentId], [Name]) VALUES (9, 8, N'Kitchen Toys')
GO
INSERT [dbo].[CategoryMaster] ([CategoryId], [ParentId], [Name]) VALUES (10, 9, N'Cooking Appliances')
GO
INSERT [dbo].[CategoryMaster] ([CategoryId], [ParentId], [Name]) VALUES (11, 9, N'Cookware')
GO
ALTER TABLE [dbo].[CategoryMaster]  WITH CHECK ADD  CONSTRAINT [FK_CategoryMaster_CategoryMaster] FOREIGN KEY([ParentId])
REFERENCES [dbo].[CategoryMaster] ([CategoryId])
GO
ALTER TABLE [dbo].[CategoryMaster] CHECK CONSTRAINT [FK_CategoryMaster_CategoryMaster]
GO

I had tried many queries but not able to get desired result. can anyone please help me out from this situation? I want output like below. enter image description here

Upvotes: 0

Views: 93

Answers (1)

sacse
sacse

Reputation: 3839

You can create a recursive function like below:

CREATE FUNCTION Create_Json(@CategoryId INT, @IsRoot INT) 
RETURNS VARCHAR(MAX)
BEGIN 
    DECLARE @Json NVARCHAR(MAX) = '{}', @Name NVARCHAR(MAX), @Children NVARCHAR(MAX)

    SET @Json =  (SELECT P.[Name],JSON_QUERY(dbo.Create_Json(P.CategoryId, 2) ) AS Children 
                    FROM [dbo].[CategoryMaster] AS P  
                    WHERE P.ParentId = @CategoryId 
                    FOR JSON AUTO);
    IF(@IsRoot = 1) 
    BEGIN
       SELECT @Name = P.[Name] FROM [dbo].[CategoryMaster] AS P WHERE P.CategoryId = @CategoryId
       SET @Json =   '"result": {"Name":"' + @Name + '","Children":' + CAST(@Json AS NVARCHAR(MAX)) + '}'
       SET @IsRoot = 0
    END
    RETURN @Json 
END 

and call it like:

select dbo.Create_Json(1, 1)

Please find the db<>fiddle here.

Upvotes: 1

Related Questions