I have data on the following structure
I want to generate nested JSON using parent-property id relation.
Desired output.
[{
"propertyID": 1
, "title": "foo"
, "class": ""
, "typeid": 150
, "value": "bar"
, "children": [{}]
}, {
"propertyID": 2
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{}]
}, {
"propertyID": 3
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{
"propertyID": 4
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{
"propertyID": 41
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{
"propertyID": 411
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{}]
}]
},{
"propertyID": 42
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{
"propertyID": 421
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{}]
}]
}]
}, {
"propertyID": 5
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{}]
}, {
"propertyID": 6
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{
"propertyID": 7
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{
"propertyID": 8
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{
"propertyID": 9
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{
"propertyID": 10
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{}]
}]
}]
}]
}]
}]
}]
I found a similar question here. But I want to do this without function. Although there are few similar questions here nothing fits into my requirement. So far I tried this. but it doesn't work when the same node with two children. it duplicates the result.
This is what I tried.
;WITH childrens AS (
SELECT propertyID, parentID, title, children
FROM CTE WHERE children = '[{}]'
union ALL
SELECT cte.propertyID, cte.parentID, cte.title, JSON_QUERY(
(SELECT c.propertyID, c.parentID, c.title, JSON_QUERY(c.children) AS children FOR JSON
PATH)
) AS children
FROM CTE
INNER JOIN childrens c on CTE.propertyID = c.parentID
), Tree as (
SELECT c.propertyID, c.parentID, c.title, JSON_QUERY(
(
SELECT ch.propertyID, ch.parentID, ch.title, JSON_QUERY(ch.children) AS children
FROM childrens ch
WHERE c.propertyID = ch.parentID FOR JSON PATH
)
) AS children
FROM childrens c WHERE parentID = 0
)
select DISTINCT *
from tree FOR JSON PATH
What I got is
[
{
"propertyID":5,
"parentID":3,
"title":"foo",
"children":[
{
}
]
},
{
"propertyID":4,
"parentID":3,
"title":"foo",
"children":[
{
"propertyID":41,
"parentID":4,
"title":"foo",
"children":[
{
"propertyID":411,
"parentID":41,
"title":"foo",
"children":[
{
}
]
}
]
}
]
},
{
"propertyID":4,
"parentID":3,
"title":"foo",
"children":[
{
"propertyID":42,
"parentID":4,
"title":"foo",
"children":[
{
"propertyID":421,
"parentID":42,
"title":"foo",
"children":[
{
}
]
}
]
}
]
},
{
"propertyID":6,
"parentID":3,
"title":"foo",
"children":[
{
"propertyID":7,
"parentID":6,
"title":"foo",
"children":[
{
"propertyID":8,
"parentID":7,
"title":"foo",
"children":[
{
"propertyID":9,
"parentID":8,
"title":"foo",
"children":[
{
"propertyID":10,
"parentID":9,
"title":"foo",
"children":[
{
}
]
}
]
}
]
}
]
}
]
}
]
It duplicates node with property = 4. any suggestions to do this.
sql fiddle sample is here
