Title: Need help by populating a PrimeReact Tree
Tags: javascript;sql;sqlite;primereact
Question: I'm trying to populate a PrimeReact Tree with data from sqlite3 database, but it doesnt work.
Here is my SQL-Query:
select 'prj:'||p.id as key,p.name as label,(
select json_group_array(json_object('key',key,'label',label))
select 'tpr:'||tpr.id as key,tpr.name as label from tpr
where tpr.prjid=p.id
and tpr.active=&quot;true&quot;
order by tpr.id
) as children
from prj p,usr_right r
where p.id=r.prjid
and p.active=&quot;true&quot;
and r.usrid=$1
order by p.id
I get following JSON-Code:
key: 'prj:1',
label: 'Projekt 1',
children: '[{&quot;key&quot;:&quot;tpr:1&quot;,&quot;label&quot;:&quot;Teilprojekt 1&quot;},{&quot;key&quot;:&quot;tpr:2&quot;,&quot;label&quot;:&quot;Teilprojekt 2&quot;}]'
{ key: 'prj:2', label: 'Projekt 3', children: '[]' },
{ key: 'prj:3', label: 'Projekt 2', children: '[]' }
This is OK but i figured out, that the children are not rendered, because there are quotes around the square brackets at the children path - how can i fix this?

Here is the accepted answer: If initial is your json to be mapped to Tree, then you need to make a slight modification to convert string to array
let initial = [
key: 'prj:1',
label: 'Projekt 1',
children: '[{"key":"tpr:1","label":"Teilprojekt 1"},{"key":"tpr:2","label":"Teilprojekt 2"}]'
{ key: 'prj:2', label: 'Projekt 3', children: '[]' },
{ key: 'prj:3', label: 'Projekt 2', children: '[]' }
let converted = initial.map(obj =&gt; ({ ...obj, children: JSON.parse(obj.children)}))
console.log(converted)
Comment for this answer: Now it works...


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:05/03/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><sql><sqlite><primereact>', 'Title': 'Need help by populating a PrimeReact Tree', 'ViewCount': 32, 'id': 72101677, 'user_AboutMe': '', 'user_AccountId': 6683309.0, 'user_DisplayName': 'Ramirez', 'user_Reputation': 19.0, 'user_Views': 88.0, 'user_id': 5155608.0}}