Create an array of JSON within a JSON in SQL











up vote
0
down vote

favorite












I have 2 tables as below and I am currently writing a function that returns a JSON based on these tables. Need help in querying this.



 TABLE1
Id ReqMode RespMode Count TabName
1001 R T 5000 TAB3
1002 R Y 10000 TAB4
1003 R T 3000 TAB6
1004 R T 5000 TAB5
1005 R Y 6000 TAB2


TABLE2
TabName CMeth CDate UMeth UDate DMeth DDate ParentTab
TAB1 F A1 F L1 C
TAB2 P C C TAB1
TAB3 P C C TAB2
TAB4 F B5 C C
TAB5 P C C TAB4
TAB6 C C C


JSON Format :



{  
"d":{
"Id":"1001",
"ReqMode":"R",
"RespMode":"T",
"Count":5000,
"TabName":"TAB3",
"DELTA":[
{
"Tabname":"TAB3",
"CMeth":"P",
"CDate":"",
"UMeth":"C",
"UDate":"",
"DMeth":"C",
"DDate":"",
"ParentTab":"TAB2"
},
{
"Tabname":"TAB2",
"CMeth":"P",
"CDate":"",
"UMeth":"C",
"UDate":"",
"DMeth":"C",
"DDate":"",
"ParentTab":"TAB1"
},
{
"Tabname":"TAB1",
"CMeth":"F",
"CDate":"A1",
"UMeth":"F",
"UDate":"L1",
"DMeth":"",
"DDate":"C",
"ParentTab":""
}
],
"MDATA":[ ]
}
}


Explanation :

If I am requesting for 'TAB3' , then I need to pull a record from TABLE1 where TabName is TAB3. Based on this table name, I check for its values in TABLE2.

My methods have values P(Parent),C(Change),F(Field)



The logic behind getting 3 rows here in my DELTA part of JSON is :

For TabName TAB3, if P is present in any of the Methods in TABLE2,then pick the ParentTab value and comapare it against TabName in TABLE2 and get its details. This is a recursive loop until we come across C or F in any of the Methods.



I am currently lost in how to create a recursive loop based on the values. My query so far :



select Id AS 'd.Id', ReqMode AS 'd.ReqMode' ,RespMode AS 'd.RespMode',Count AS 'd.Count',TabName AS 'd.TabName'
,JSON_QUERY('') 'd.DELTA',JSON_QUERY('') 'd.MDATA'
FROM TABLE1
FOR JSON PATH


Need help in creating DELTA part of my JSON which will depend on the TabName I pass as an input to the function.



Thanks in advance.










share|improve this question






















  • For those that would like some useful sample data: db<>fiddle
    – Larnu
    Nov 20 at 14:56















up vote
0
down vote

favorite












I have 2 tables as below and I am currently writing a function that returns a JSON based on these tables. Need help in querying this.



 TABLE1
Id ReqMode RespMode Count TabName
1001 R T 5000 TAB3
1002 R Y 10000 TAB4
1003 R T 3000 TAB6
1004 R T 5000 TAB5
1005 R Y 6000 TAB2


TABLE2
TabName CMeth CDate UMeth UDate DMeth DDate ParentTab
TAB1 F A1 F L1 C
TAB2 P C C TAB1
TAB3 P C C TAB2
TAB4 F B5 C C
TAB5 P C C TAB4
TAB6 C C C


JSON Format :



{  
"d":{
"Id":"1001",
"ReqMode":"R",
"RespMode":"T",
"Count":5000,
"TabName":"TAB3",
"DELTA":[
{
"Tabname":"TAB3",
"CMeth":"P",
"CDate":"",
"UMeth":"C",
"UDate":"",
"DMeth":"C",
"DDate":"",
"ParentTab":"TAB2"
},
{
"Tabname":"TAB2",
"CMeth":"P",
"CDate":"",
"UMeth":"C",
"UDate":"",
"DMeth":"C",
"DDate":"",
"ParentTab":"TAB1"
},
{
"Tabname":"TAB1",
"CMeth":"F",
"CDate":"A1",
"UMeth":"F",
"UDate":"L1",
"DMeth":"",
"DDate":"C",
"ParentTab":""
}
],
"MDATA":[ ]
}
}


Explanation :

If I am requesting for 'TAB3' , then I need to pull a record from TABLE1 where TabName is TAB3. Based on this table name, I check for its values in TABLE2.

My methods have values P(Parent),C(Change),F(Field)



The logic behind getting 3 rows here in my DELTA part of JSON is :

For TabName TAB3, if P is present in any of the Methods in TABLE2,then pick the ParentTab value and comapare it against TabName in TABLE2 and get its details. This is a recursive loop until we come across C or F in any of the Methods.



I am currently lost in how to create a recursive loop based on the values. My query so far :



select Id AS 'd.Id', ReqMode AS 'd.ReqMode' ,RespMode AS 'd.RespMode',Count AS 'd.Count',TabName AS 'd.TabName'
,JSON_QUERY('') 'd.DELTA',JSON_QUERY('') 'd.MDATA'
FROM TABLE1
FOR JSON PATH


Need help in creating DELTA part of my JSON which will depend on the TabName I pass as an input to the function.



Thanks in advance.










share|improve this question






















  • For those that would like some useful sample data: db<>fiddle
    – Larnu
    Nov 20 at 14:56













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have 2 tables as below and I am currently writing a function that returns a JSON based on these tables. Need help in querying this.



 TABLE1
Id ReqMode RespMode Count TabName
1001 R T 5000 TAB3
1002 R Y 10000 TAB4
1003 R T 3000 TAB6
1004 R T 5000 TAB5
1005 R Y 6000 TAB2


TABLE2
TabName CMeth CDate UMeth UDate DMeth DDate ParentTab
TAB1 F A1 F L1 C
TAB2 P C C TAB1
TAB3 P C C TAB2
TAB4 F B5 C C
TAB5 P C C TAB4
TAB6 C C C


JSON Format :



{  
"d":{
"Id":"1001",
"ReqMode":"R",
"RespMode":"T",
"Count":5000,
"TabName":"TAB3",
"DELTA":[
{
"Tabname":"TAB3",
"CMeth":"P",
"CDate":"",
"UMeth":"C",
"UDate":"",
"DMeth":"C",
"DDate":"",
"ParentTab":"TAB2"
},
{
"Tabname":"TAB2",
"CMeth":"P",
"CDate":"",
"UMeth":"C",
"UDate":"",
"DMeth":"C",
"DDate":"",
"ParentTab":"TAB1"
},
{
"Tabname":"TAB1",
"CMeth":"F",
"CDate":"A1",
"UMeth":"F",
"UDate":"L1",
"DMeth":"",
"DDate":"C",
"ParentTab":""
}
],
"MDATA":[ ]
}
}


Explanation :

If I am requesting for 'TAB3' , then I need to pull a record from TABLE1 where TabName is TAB3. Based on this table name, I check for its values in TABLE2.

My methods have values P(Parent),C(Change),F(Field)



The logic behind getting 3 rows here in my DELTA part of JSON is :

For TabName TAB3, if P is present in any of the Methods in TABLE2,then pick the ParentTab value and comapare it against TabName in TABLE2 and get its details. This is a recursive loop until we come across C or F in any of the Methods.



I am currently lost in how to create a recursive loop based on the values. My query so far :



select Id AS 'd.Id', ReqMode AS 'd.ReqMode' ,RespMode AS 'd.RespMode',Count AS 'd.Count',TabName AS 'd.TabName'
,JSON_QUERY('') 'd.DELTA',JSON_QUERY('') 'd.MDATA'
FROM TABLE1
FOR JSON PATH


Need help in creating DELTA part of my JSON which will depend on the TabName I pass as an input to the function.



Thanks in advance.










share|improve this question













I have 2 tables as below and I am currently writing a function that returns a JSON based on these tables. Need help in querying this.



 TABLE1
Id ReqMode RespMode Count TabName
1001 R T 5000 TAB3
1002 R Y 10000 TAB4
1003 R T 3000 TAB6
1004 R T 5000 TAB5
1005 R Y 6000 TAB2


TABLE2
TabName CMeth CDate UMeth UDate DMeth DDate ParentTab
TAB1 F A1 F L1 C
TAB2 P C C TAB1
TAB3 P C C TAB2
TAB4 F B5 C C
TAB5 P C C TAB4
TAB6 C C C


JSON Format :



{  
"d":{
"Id":"1001",
"ReqMode":"R",
"RespMode":"T",
"Count":5000,
"TabName":"TAB3",
"DELTA":[
{
"Tabname":"TAB3",
"CMeth":"P",
"CDate":"",
"UMeth":"C",
"UDate":"",
"DMeth":"C",
"DDate":"",
"ParentTab":"TAB2"
},
{
"Tabname":"TAB2",
"CMeth":"P",
"CDate":"",
"UMeth":"C",
"UDate":"",
"DMeth":"C",
"DDate":"",
"ParentTab":"TAB1"
},
{
"Tabname":"TAB1",
"CMeth":"F",
"CDate":"A1",
"UMeth":"F",
"UDate":"L1",
"DMeth":"",
"DDate":"C",
"ParentTab":""
}
],
"MDATA":[ ]
}
}


Explanation :

If I am requesting for 'TAB3' , then I need to pull a record from TABLE1 where TabName is TAB3. Based on this table name, I check for its values in TABLE2.

My methods have values P(Parent),C(Change),F(Field)



The logic behind getting 3 rows here in my DELTA part of JSON is :

For TabName TAB3, if P is present in any of the Methods in TABLE2,then pick the ParentTab value and comapare it against TabName in TABLE2 and get its details. This is a recursive loop until we come across C or F in any of the Methods.



I am currently lost in how to create a recursive loop based on the values. My query so far :



select Id AS 'd.Id', ReqMode AS 'd.ReqMode' ,RespMode AS 'd.RespMode',Count AS 'd.Count',TabName AS 'd.TabName'
,JSON_QUERY('') 'd.DELTA',JSON_QUERY('') 'd.MDATA'
FROM TABLE1
FOR JSON PATH


Need help in creating DELTA part of my JSON which will depend on the TabName I pass as an input to the function.



Thanks in advance.







sql json sql-server azure-sql-database






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 14:42









user1483122

124110




124110












  • For those that would like some useful sample data: db<>fiddle
    – Larnu
    Nov 20 at 14:56


















  • For those that would like some useful sample data: db<>fiddle
    – Larnu
    Nov 20 at 14:56
















For those that would like some useful sample data: db<>fiddle
– Larnu
Nov 20 at 14:56




For those that would like some useful sample data: db<>fiddle
– Larnu
Nov 20 at 14:56












1 Answer
1






active

oldest

votes

















up vote
0
down vote













This answer is a little late, but this should give you what you're looking for. It is important to note recursive cte's have a default limit of 100, but you can use the MAXRECURSION option to configure a reasonable limit for your situation.



declare @id int = 1001
declare @tab varchar(100) = (select tabName from Table1 where id = @id);

with delta as (
select tabname,cmeth,isnull(cdate,'') as cdate
,umeth,isnull(udate,'') as udate
,dmeth,isnull(ddate,'') as ddate
,isnull(parenttab,'') as parenttab
from dbo.Table2
where tabname = @tab
union all
select t.tabname,t.cmeth,isnull(t.cdate,'') as cdate
,t.umeth,isnull(t.udate,'') as udate
,t.dmeth,isnull(t.ddate,'') as ddate
,isnull(t.parenttab,'') as parenttab
from dbo.Table2 t
inner join delta d on t.tabname = d.parenttab
)
SELECT ID as 'd.id'
,ReqMode as 'd.reqmode'
,RespMode as 'd.respmode'
,[Count] as 'd.count'
,TabName as 'd.tabname'
,json_query((select distinct * from delta for json path)) as delta
,json_query('') as mdata
FROM dbo.Table1 T1
WHERE ID = @id
FOR JSON PATH;
go





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395467%2fcreate-an-array-of-json-within-a-json-in-sql%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    This answer is a little late, but this should give you what you're looking for. It is important to note recursive cte's have a default limit of 100, but you can use the MAXRECURSION option to configure a reasonable limit for your situation.



    declare @id int = 1001
    declare @tab varchar(100) = (select tabName from Table1 where id = @id);

    with delta as (
    select tabname,cmeth,isnull(cdate,'') as cdate
    ,umeth,isnull(udate,'') as udate
    ,dmeth,isnull(ddate,'') as ddate
    ,isnull(parenttab,'') as parenttab
    from dbo.Table2
    where tabname = @tab
    union all
    select t.tabname,t.cmeth,isnull(t.cdate,'') as cdate
    ,t.umeth,isnull(t.udate,'') as udate
    ,t.dmeth,isnull(t.ddate,'') as ddate
    ,isnull(t.parenttab,'') as parenttab
    from dbo.Table2 t
    inner join delta d on t.tabname = d.parenttab
    )
    SELECT ID as 'd.id'
    ,ReqMode as 'd.reqmode'
    ,RespMode as 'd.respmode'
    ,[Count] as 'd.count'
    ,TabName as 'd.tabname'
    ,json_query((select distinct * from delta for json path)) as delta
    ,json_query('') as mdata
    FROM dbo.Table1 T1
    WHERE ID = @id
    FOR JSON PATH;
    go





    share|improve this answer

























      up vote
      0
      down vote













      This answer is a little late, but this should give you what you're looking for. It is important to note recursive cte's have a default limit of 100, but you can use the MAXRECURSION option to configure a reasonable limit for your situation.



      declare @id int = 1001
      declare @tab varchar(100) = (select tabName from Table1 where id = @id);

      with delta as (
      select tabname,cmeth,isnull(cdate,'') as cdate
      ,umeth,isnull(udate,'') as udate
      ,dmeth,isnull(ddate,'') as ddate
      ,isnull(parenttab,'') as parenttab
      from dbo.Table2
      where tabname = @tab
      union all
      select t.tabname,t.cmeth,isnull(t.cdate,'') as cdate
      ,t.umeth,isnull(t.udate,'') as udate
      ,t.dmeth,isnull(t.ddate,'') as ddate
      ,isnull(t.parenttab,'') as parenttab
      from dbo.Table2 t
      inner join delta d on t.tabname = d.parenttab
      )
      SELECT ID as 'd.id'
      ,ReqMode as 'd.reqmode'
      ,RespMode as 'd.respmode'
      ,[Count] as 'd.count'
      ,TabName as 'd.tabname'
      ,json_query((select distinct * from delta for json path)) as delta
      ,json_query('') as mdata
      FROM dbo.Table1 T1
      WHERE ID = @id
      FOR JSON PATH;
      go





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        This answer is a little late, but this should give you what you're looking for. It is important to note recursive cte's have a default limit of 100, but you can use the MAXRECURSION option to configure a reasonable limit for your situation.



        declare @id int = 1001
        declare @tab varchar(100) = (select tabName from Table1 where id = @id);

        with delta as (
        select tabname,cmeth,isnull(cdate,'') as cdate
        ,umeth,isnull(udate,'') as udate
        ,dmeth,isnull(ddate,'') as ddate
        ,isnull(parenttab,'') as parenttab
        from dbo.Table2
        where tabname = @tab
        union all
        select t.tabname,t.cmeth,isnull(t.cdate,'') as cdate
        ,t.umeth,isnull(t.udate,'') as udate
        ,t.dmeth,isnull(t.ddate,'') as ddate
        ,isnull(t.parenttab,'') as parenttab
        from dbo.Table2 t
        inner join delta d on t.tabname = d.parenttab
        )
        SELECT ID as 'd.id'
        ,ReqMode as 'd.reqmode'
        ,RespMode as 'd.respmode'
        ,[Count] as 'd.count'
        ,TabName as 'd.tabname'
        ,json_query((select distinct * from delta for json path)) as delta
        ,json_query('') as mdata
        FROM dbo.Table1 T1
        WHERE ID = @id
        FOR JSON PATH;
        go





        share|improve this answer












        This answer is a little late, but this should give you what you're looking for. It is important to note recursive cte's have a default limit of 100, but you can use the MAXRECURSION option to configure a reasonable limit for your situation.



        declare @id int = 1001
        declare @tab varchar(100) = (select tabName from Table1 where id = @id);

        with delta as (
        select tabname,cmeth,isnull(cdate,'') as cdate
        ,umeth,isnull(udate,'') as udate
        ,dmeth,isnull(ddate,'') as ddate
        ,isnull(parenttab,'') as parenttab
        from dbo.Table2
        where tabname = @tab
        union all
        select t.tabname,t.cmeth,isnull(t.cdate,'') as cdate
        ,t.umeth,isnull(t.udate,'') as udate
        ,t.dmeth,isnull(t.ddate,'') as ddate
        ,isnull(t.parenttab,'') as parenttab
        from dbo.Table2 t
        inner join delta d on t.tabname = d.parenttab
        )
        SELECT ID as 'd.id'
        ,ReqMode as 'd.reqmode'
        ,RespMode as 'd.respmode'
        ,[Count] as 'd.count'
        ,TabName as 'd.tabname'
        ,json_query((select distinct * from delta for json path)) as delta
        ,json_query('') as mdata
        FROM dbo.Table1 T1
        WHERE ID = @id
        FOR JSON PATH;
        go






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 29 at 20:55









        ashja99

        7327




        7327






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395467%2fcreate-an-array-of-json-within-a-json-in-sql%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'