Learning Journey
I am currently teaching myself JavaScript and exploring JSON along the way. My current project involves developing a JavaScript WebScraper where I plan to store my results in JSON format.
While I am aware of other methods like using databases, server-client interactions, etc., my primary focus at the moment is on understanding JSON - how to parse, create, and format it. This serves as a valuable learning opportunity for me.
Understanding Variables
The data stored in the aforementioned variables originates from an HTML file. To illustrate, here's an example of the content:
users[] -> "<a href=\"countries.php?id=1\">Egypt</a>"
GDP[] -> "<td> $2,971</td>"
Regions[] -> "<td> Egypt </td>"
Align[] -> "<td> Eastern Bloc </td>"
Creative Coding
let countries = [];
for(let i = 0; i < users.length; i++)
{
countries.push( {
'country' : [{
'name' : users[i],
'GDP' : GDP[i],
'Region' : regions[i],
'Align' : align[i]
}]})
};
let obj_data = JSON.stringify(countries, null, 2);
fs.writeFileSync('countryballs.json', obj_data);
Decoding the Code
Prior to this code snippet, I had populated arrays (users, GDP, regions, align)
with data extracted in string format from a website. The subsequent idea was to encapsulate this data into an object and utilize the stringify() function to convert it into JSON.
Upon testing this implementation without the loop (with static test data), it proved to be functional.
Error Investigation
let obj_data = JSON.stringify(countries, null, 2);
^
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Node'
| property 'children' -> object with constructor 'Array'
| index 0 -> object with constructor 'Node'
--- property 'parent' closes the circle
Query Clarification
My goal with this question is to comprehend what causes this JSON format to become "Circular" and strategize solutions to make this code align with my objectives.
End Notes
- I'm working within Node.js and Visual Studio Code environment
Additional Insights
Successfully Tested Code Snippet
let countries;
console.log(users.length)
for(let i = 0; i < users.length; i++)
{
countries = {
country : [
{
"name" : 'CountryTest'
}
]
}
};
let obj_data = JSON.stringify(countries, null, 2);
fs.writeFileSync('countryballs.json', obj_data);
});
Comparing this revised code sample to the initial version, note that I manually input the country name here. This approach has proven to work flawlessly:
https://i.sstatic.net/xtVF2.jpg
By substituting 'CountryTest' with `users[i]` containing country names, the circular error emerges once more, prompting a partial solution by adding `+""`:
Enhanced Code Example:
for(let i = 0; i < users.length; i++)
{
countries = {
country : [
{
"name" : users[i]+''
}
]
}
};
This adjustment leads to:
https://i.sstatic.net/ehIec.jpg
Unresolved Bug
Despite these modifications, only one country is displayed out of the 32 present in the `users[]` array. This discrepancy raises doubts regarding the provided solutions so far.
Preferred JSON Structure
{
"countries": {
"country": [
{
"name": "",
"GDP" : "",
"Region" : "",
"Align" : ""
},
{
"name": "",
"GDP" : "",
"Region" : "",
"Align" : ""
},
{
"name": "",
"GDP" : "",
"Region" : "",
"Align" : ""
}
]}
}