I received JSON data from my API endpoint with a specific structure:
[
{
"id": 1,
"title": "Smells like",
"object": "",
"children": [
{
"id": 2,
"title": "Liquid",
"object": {
"objectid": 1,
"title": "My object also have children",
"children": []
},
"children": [
{
"id": 3,
"title": "Loyal",
"object": "",
"children": []
},
{
"id": 4,
"title": "Smart",
"object": "",
"children": [
{
"id": 5,
"title": "Smart",
"object": "",
"children": []
}
]
}
]
},
{
"id": 6,
"title": "Just empty",
"object": {
"objectid": 2,
"title": "Title of my object",
"children": []
},
"children": []
}
]
},
{
"id": 10,
"title": "Apple",
"object": "",
"children": [
{
"id": 11,
"title": "Ink",
"object": "",
"children": []
},
{
"id": 12,
"title": "Whatever",
"object": "",
"children": []
}
]
}
]
To convert this JSON to a format compatible with the Angular component I am using, follow this structure:
[
new Link({
id: 1,
label: "Smells like",
title: "Smells like",
object: "",
children: [
new Link({
id: 2,
label: "Liquid",
title: "Liquid",
object: {
objectid: 1,
title: "My object also have children",
children: [],
},
children: [
new Link({
id: 3,
label: "Loyal",
title: "Loyal",
object: "",
children: [],
}),
new Link({
id: 4,
label: "Smart",
title: "Smart",
object: "",
children: [
new Link({
id: 5,
label: "test",
title: "test",
object: "",
children: [],
}),
],
}),
],
}),
new Link({
id: 6,
label: "Just empty",
title: "Just empty",
object: { objectid: 2, title: "Title of my object", children: [] },
children: [],
}),
],
}),
new Link({
id: 10,
label: "Apple",
title: "Apple",
object: "",
children: [
new Link({ id: 11, title: "Ink", object: "", children: [] }),
new Link({
id: 12,
label: "Whatever",
title: "Whatever",
object: "",
children: [],
}),
],
}),
];
Some key points:
- The new structure wraps root and children nodes with a new Link()
- A label is added based on the title of each node
- Sometimes, other objects in the JSON might have children properties where a new link wrapper is not necessary.
If you need help converting the first JSON structure to the required format in TypeScript, feel free to ask!