+ 1

Create JSON hierarchy in JavaScript?

I have an employee table with columns userid and manager id, i need to create hierarchical json string using javascript.

6th May 2018, 5:28 PM
RJ Negi
RJ Negi - avatar
9 Réponses
+ 2
Hi RJ how big is the data file, and how is it held? or this just an excercise JS?
8th May 2018, 4:56 PM
Mike Choy
Mike Choy - avatar
+ 2
In which case you probably better off setting up a free account on MLAB and create an instance of MongoDB, that you can drive using node JS. That way you can import a CSV file in and it will be automatically held in a JSON like format.
8th May 2018, 5:16 PM
Mike Choy
Mike Choy - avatar
+ 1
Hi RJ Are you looking for something like this ? console.log(JSON.stringify( {"employee":[ {user:"John", manager:"Joe"}, {user:"Steve", manager:"Jane"}, {user:"Peter", manager:"John"}, {user:"Chaitanya", manager:"Freddy"} ] }) ) and heirarchical version console.log(JSON.stringify( {"employee":[ {manager:"John",user:{userid:"mike"}}, {manager:"Jane",user:{userid:"sean"}}, {manager:"Fred",user:{userid:"chaitanya"}} ] }) )
6th May 2018, 10:05 PM
Mike Choy
Mike Choy - avatar
0
Where manager id is tha parent and userid is a child... Kind of recursive data....
6th May 2018, 5:28 PM
RJ Negi
RJ Negi - avatar
0
JSON data Something like this---- { "Name": "Bernadette", "ID": "30466", "Children": [{ "Name": "Pagen", "ID": "15933" }, { "Name": "Ronald", "ID": "18682", "Children": [{ "Name": "Konrad", "ID": "18788" }, { "Name": "Paul", "ID": "23211" }] }, { "Name": "Elizabeth", "ID": "27301" }]} - The Data is like: where Sponsor ID is the Parent of CustomerID-- CUSTOMERID FIRSTNAME SPONSORID SPONSORNAME 4640 Sarah 30521 Peter 5165 Krysia 4871 Graham 6564 John 4871 Graham 13716 Narelle 23675 Gary 15933 Pagen 30466 Bernadette 18096 Magdolna 30505 Elizabeth 18108 Evelyn 30542 Sylvie 18531 Janice 18191 Kaye 18682 Ronald 30477 Cynthia i want to do this in java script any help ?
8th May 2018, 6:31 AM
RJ Negi
RJ Negi - avatar
0
The data is in sql server... The hierarchy can go upto n levels..
8th May 2018, 4:58 PM
RJ Negi
RJ Negi - avatar
0
the requirement ia such that i have to this in javascript.... I am using a reporting tool to communicate with database.. Crearing a flat json data of the table in sql..
8th May 2018, 5:19 PM
RJ Negi
RJ Negi - avatar
0
Hi RJ Of course I'm not going to do your homework for you but here is some code that might help you. You can change the format easily using template literals. I've just done one line of the table but flat. You can have a go at multiple lines and nesting ;-) const employees =[4640,"Sarah",30521,"Peter"]; const headings=["CUSTOMERID","FIRSTNAME","SPONSORID","SPONSORNAME"]; let output=[]; //convert array to a JSON formatted string using template literals output = employees.map((x,index)=>`${headings[index]} :${x}`); //convert array to string tString=output.join(); //top and tail with curly brackets using template literals jString=`{ ${tString} }`; //we have our JSON String console.log(jString);
8th May 2018, 6:33 PM
Mike Choy
Mike Choy - avatar
0
Its very obvious Mike that i have to do it myself... Actually i have achieved it in SQL Server was just crious about js... Anyways thanks.... :)
9th May 2018, 2:32 AM
RJ Negi
RJ Negi - avatar