0

Generate JSON in such a way that JSON for child table should have ARRAY_WRAPPER and JSON for parent table should NOT have ARRAY_WRAPPER and Query should be on PARENT1 table on column P1NAME in ('kumar,pathan'), so output should have 2 rows

I have below mentioned tables

CREATE TABLE [dbo].[Z_PARENT1](
    [P1id] [int] IDENTITY(1,1) NOT NULL,
    [P1NAME] [varchar](50) NULL
)

CREATE TABLE [dbo].[Z_PARENT2](
    [P2id] [int] IDENTITY(1,1) NOT NULL,
    [P1id] [int] NOT NULL,
    [P2NAME] [varchar](50) NULL
)

CREATE TABLE [dbo].[Z_CHILD](
    [Cid] [int] IDENTITY(1,1) NOT NULL,
    [P1id] [int] NOT NULL,
    [CNAME] [varchar](50) NULL
)

INSERT INTO [dbo].[Z_PARENT1] ([P1NAME]) VALUES ('kumar'), ('pathan') , ('chris')
INSERT INTO [dbo].[Z_PARENT2] ([P1id],[P2NAME]) VALUES (1,'Mrs.kumar'), (2,'Mrs.pathan') , (3,'Mrs.chris')
INSERT INTO [dbo].[Z_CHILD] ([P1id],[CNAME]) VALUES (1,'A_kumar'),(1,'B_kumar'),(2,'X_pathan'),(2,'Y_pathan') 

Query should be on PARENT1 table on column P1NAME in ('kumar,pathan'), so Query Output should have 2 rows,
Row1 should be like below

{
  "PARENT1":{"P1id":1,"P1NAME":"kumar"},
  "PARENT2":{"P2NAME":"Mrs.kumar"},
  "CHILD":[{"Cid":1,"P1id":1,"CNAME":"A_kumar"},{"Cid":2,"P1id":1,"CNAME":"B_kumar"}]
}

Row2 should be like below

{
  "PARENT1":{"P1id":2,"P1NAME":"pathan"},
  "PARENT2":{"P2NAME":"Mrs.pathan"},
  "CHILD":[{"Cid":3,"P1id":2,"CNAME":"X_pathan"},{"Cid":4,"P1id":2,"CNAME":"Y_pathan"}]
}

Please note that:(Important)

  1. i should be able to query on PARENT1 table on column P1NAME IN ('kumar,pathan') condition, so output should have 2 rows
  2. only child should have Arrays in JSON. ( i mean ARRAY_WRAPPER ) and parent1 & parent2 should NOT have Arrays (i mean WITHOUT_ARRAY_WRAPPER)
  3. there should be left join b/w parent & child coz if child has no rows atleast parent JSON should be constructed.
  4. as parent1 & parent2 are master tables, there should be inner join on pid's
user2192023
  • 1,601
  • 2
  • 11
  • 12
  • This is duplicate question: https://stackoverflow.com/questions/65039335/json-for-multiple-parent-tables-and-one-child-table – Emin Mesic Nov 27 '20 at 17:26
  • If you think that this or any other answer is the best solution to your problem, you may [accept](https://stackoverflow.com/help/accepted-answer) it. Only one answer can be accepted. – Zhorov Jul 14 '21 at 06:31

1 Answers1

0

Original answer:

A possible approach is the following statement. You need to generate a valid JSON for each row, using FOR JSON PATH with the appropriate paths and an additional OUTER APPLY operator.

Test data (before the change in the requirements):

CREATE TABLE [dbo].[Z_PARENT](
    [Pid] [int] IDENTITY(1,1) NOT NULL,
    [PNAME] [varchar](50) NULL
)
CREATE TABLE [dbo].[Z_CHILD](
    [Cid] [int] IDENTITY(1,1) NOT NULL,
    [Pid] [int] NOT NULL,
    [CNAME] [varchar](50) NULL
)

INSERT INTO [dbo].[Z_PARENT] ([PNAME]) VALUES ('kumar')
INSERT INTO [dbo].[Z_PARENT] ([PNAME]) VALUES ('pathan')
INSERT INTO [dbo].[Z_PARENT] ([PNAME]) VALUES ('abcd')
INSERT INTO [dbo].[Z_CHILD] ([Pid],[CNAME]) VALUES (1,'A_kumar')
INSERT INTO [dbo].[Z_CHILD] ([Pid],[CNAME]) VALUES (1,'B_kumar')
INSERT INTO [dbo].[Z_CHILD] ([Pid],[CNAME]) VALUES (2,'X_pathan')
INSERT INTO [dbo].[Z_CHILD] ([Pid],[CNAME]) VALUES (2,'Y_pathan')

Statement:

SELECT (
   SELECT 
      p.Pid AS 'PARENT.Pid', 
      p.PNAME AS 'PARENT.PNAME',
      c.Json AS 'CHILD'
   FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
) AS JsonOutput
FROM dbo.Z_Parent p
OUTER APPLY (
   SELECT Cid, Pid, CNAME
   FROM dbo.Z_CHILD
   WHERE Pid = p.Pid
   FOR JSON AUTO
) c (Json)

Result (using the test data from the question):

JsonOutput
{"PARENT":{"Pid":1,"PNAME":"kumar"},"CHILD":[{"Cid":1,"Pid":1,"CNAME":"A_kumar"},{"Cid":2,"Pid":1,"CNAME":"B_kumar"}]}
{"PARENT":{"Pid":2,"PNAME":"pathan"},"CHILD":[{"Cid":3,"Pid":2,"CNAME":"X_pathan"},{"Cid":4,"Pid":2,"CNAME":"Y_pathan"}]}

Update:

If you need to add another table as a source and a WHERE clause, try the following:

SELECT (
   SELECT 
      p1.P1id AS 'PARENT1.P1id', 
      p1.P1NAME AS 'PARENT1.P1NAME',
      p2.P2NAME AS 'PARENT2.P2NAME',
      c.Json AS 'CHILD'
   FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
) AS JsonOutput
FROM dbo.Z_PARENT1 p1
INNER JOIN dbo.Z_PARENT2 p2 ON p1.P1Id = p2.P1id
OUTER APPLY (
   SELECT Cid, P1id, CNAME
   FROM dbo.Z_CHILD
   WHERE P1id = p1.P1id
   FOR JSON AUTO
) c (Json)
WHERE p1.P1id IN (SELECT TRY_CONVERT(int, [value]) FROM STRING_SPLIT('1,2', ','))
Zhorov
  • 28,486
  • 6
  • 27
  • 52
  • Sorry, my requirement changed, so i changed the question totally. Can you please share answer accordingly. ? – user2192023 Nov 27 '20 at 14:42
  • @user2192023 You should post another question, if the requirements are totally different. – Zhorov Nov 27 '20 at 14:42
  • In your UPDATED reply/query, how i can add this condition:- where p1.P1Id in (SELECT value FROM STRING_SPLIT('1,2', ',')) – user2192023 Nov 27 '20 at 15:30