I am looking to transform my JSON array data into a structured tree format. The current JSON data is in the form:
"data": {
"name": "Sint Maarten",
"introduction": {...},
"geography": {...}
},
I want to convert it to something like this:
{"nodes":[{"id":'Sint Maarten'},{"id":'Hemisphere'}, {"id": "N"}, {"id":"E"}], "links": [{source:"Sint Maarten", target:"Hemisphere"}, {source:"Hemisphere", target: "N"}, {source: "Hemisphere", target: "E"}]}
My goal is to visualize it as a tree structure, similar to this:
Sint Maarten ---> Hemisphere ---> [N,E]
---> Language ---> [Language 1, L2, L3]
I have attempted manually flattening the JSON object array and adding elements into nodes but I need help with creating the links correctly due to the nested nature of the data. Is there a technique or algorithm to achieve this hierarchical visualization for my dataset?
Any advice on how to approach this problem would be greatly appreciated.
EDIT: Here is my current code snippet:
let rawData = fs.readFileSync('./database/factbook.json')
let picker= JSON.parse(rawData)
let arr= {}
arr['name'] = picker['name']
arr['hemisphere'] = [picker['geography']['geographic_coordinates']['latitude']['hemisphere'], picker['geography']['geographic_coordinates']['longitude']['hemisphere']]
arr['area'] = '/'
...
...
I am trying to create an array with the desired output by selecting specific parameters from the raw file, as it contains many fields but I am only interested in a subset. Once I have this array, I need to organize the key-value pairs into a node-links structure that accounts for arrays within some key values.
key: [a,b,c]
If a key value is an array, the expected output in links should reflect each element of the array.
key --> a
key ---> b
key ---> c
``