Json oluşturmak için Farklı bir Metotdda ben paylaşayım :
tsql -> for json auto ile bir ihtiyaçtan hasıl olan 3 farklı tablodan iç içe geçmiş nested related child json object oluşturma kodunu örnekleyip paylaşıyorum.
bu mantıkla çok daha fazla derinlikli json objectler de oluşturabilirsiniz.
Önce tsql cursorlar ile yapayim dedim , sonra datasetlerle falan, ama bi yığın koda döngülere gerek yok diye daha sade olan bu birkaç satırlık sqli yazdım. Belki işinize yarar.
önce test amaçlı ilişkisel örnek verimizi oluşturuyoruz.
bu üstteki 3 tabloyu bağlayarak iç içe geçmiş 3 seviyeli nested related child json objectimizi oluşturuyoruz.
Sonuç :
tsql -> for json auto ile bir ihtiyaçtan hasıl olan 3 farklı tablodan iç içe geçmiş nested related child json object oluşturma kodunu örnekleyip paylaşıyorum.
bu mantıkla çok daha fazla derinlikli json objectler de oluşturabilirsiniz.
Önce tsql cursorlar ile yapayim dedim , sonra datasetlerle falan, ama bi yığın koda döngülere gerek yok diye daha sade olan bu birkaç satırlık sqli yazdım. Belki işinize yarar.
önce test amaçlı ilişkisel örnek verimizi oluşturuyoruz.
declare @t1 table ( ad varchar(30) ) insert into @t1 values ('abdullah') insert into @t1 values ('Emrah') insert into @t1 values ('Emrullah') declare @t2 table ( ad varchar(30) , yas int ) insert into @t2 values ('abdullah' ,30) insert into @t2 values ('abdullah' ,31) insert into @t2 values ('Emrah' ,40) insert into @t2 values ('Emrah' ,41) insert into @t2 values ('Emrullah' ,90) insert into @t2 values ('Emrullah' ,91) declare @t3 table ( ad varchar(30) , yas int , op int ) insert into @t3 values ('abdullah' ,30 , 300) insert into @t3 values ('abdullah' ,30 , 301) insert into @t3 values ('abdullah' ,30 , 302) insert into @t3 values ('abdullah' ,31 , 310) insert into @t3 values ('abdullah' ,31 , 311) insert into @t3 values ('abdullah' ,31 , 312) insert into @t3 values ('Emrah' ,40 , 401) insert into @t3 values ('Emrah' ,40 , 402) insert into @t3 values ('Emrah' ,41 , 403) insert into @t3 values ('Emrah' ,41 , 404) insert into @t3 values ('Emrullah' ,90 , 901) insert into @t3 values ('Emrullah' ,90 , 902) insert into @t3 values ('Emrullah' ,91 , 903) insert into @t3 values ('Emrullah' ,91 , 904)
bu üstteki 3 tabloyu bağlayarak iç içe geçmiş 3 seviyeli nested related child json objectimizi oluşturuyoruz.
select * from ( select t1.ad , ( select yas , ( select op from @t3 t3 where t3.ad = t2.ad and t2.yas = t3.yas for json auto, include_null_values ) child from @t2 t2 where t2.ad = t1.ad for json auto, include_null_values ) child from @t1 t1 ) q for json auto, include_null_values
Sonuç :
[ {"ad":"abdullah","child":[ {"yas":30,"child": [ {"op":300}, {"op":301}, {"op":302} ]}, {"yas":31,"child": [ {"op":310}, {"op":311}, {"op":312} ] } ] }, {"ad":"Emrah","child":[{"yas":40,"child":[{"op":401},{"op":402}]},{"yas":41,"child":[{"op":403},{"op":404}]}]}, {"ad":"Emrullah","child":[{"yas":90,"child":[{"op":901},{"op":902}]},{"yas":91,"child":[{"op":903},{"op":904}]}]} ]
İmkanın sınırı, imkansızın yanıbaşındadır. Denemeden bilemezsin.