Reputation: 377
I am do not know much about javascript or JSON and I am not sure why this is not working.
A level of my JSON is below, but it basically is
[{'Africa' : {'children':[{stuff}], 'name':Africa} ...other continents
The pattern is always the same, where the a dict key is defined, in it is children and its name.
When I load it into d3.json, I get the error Uncaught TypeError: Cannot read property '0' of undefined.
d3.json("data/output.json", function(error, json) {
root = json[0];
update(root);
});
What am I doing wrong? Is there a problem with my my json or do I need to add more detail in the d3.json function?
I generated my json in python like this
def build_tree(d, val):
return [{id_:{'name': name, 'children': build_tree(d, id_)} for id_, name in d[val]}]
My JSON is this
[{'Africa': {'children': [{'Egypt': {'children': [{'EG-Cables': {'children': [{'H03VVH2F2x0.75mm': {'children': [{'2.5A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'},
'H05RRF3G1.0mm': {'children': [{'10A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'},
'H05VVF2x1.00mm': {'children': [{'10A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'},
'H05VVF3G0.75mm': {'children': [{'2.5A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'},
'H05VVF3G1.0mm': {'children': [{'10A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'},
'H05VVF3G1.5mm': {'children': [{'16A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'}}],
'name': 'Products'},
'EG-Plug': {'children': [{'BS1363': {'children': [{'13A/V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Plug'},
'CEE-7/XVLL': {'children': [{'16A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Plug'},
'CEE-XVLL': {'children': [{'10A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Plug'},
'EN-50075-XVL': {'children': [{'2.5A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Plug'}}],
'name': 'Products'}}],
'name': 'Country'},
'Nigeria': {'children': [{'NG-Cables': {'children': [{'H03VVH2F2x0.75mm': {'children': [{'2.5A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'},
'H05RRF3G1.0mm': {'children': [{'10A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'},
'H05VVF2x1.00mm': {'children': [{'10A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'},
'H05VVF3G0.75mm': {'children': [{'2.5A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'},
'H05VVF3G1.0mm': {'children': [{'10A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'},
'H05VVF3G1.5mm': {'children': [{'16A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'}}],
'name': 'Products'},
'NG-Plug': {'children': [{'BS1363': {'children': [{'13A/V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Plug'},
'SABS-164/1:1992': {'children': [{'16A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Plug'}}],
'name': 'Products'}}],
'name': 'Country'},
'South-Africa': {'children': [{'ZA-Cables': {'children': [{'H03VVH2F2x0.75mm': {'children': [{'2.5A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'},
'H05RRF3G1.0mm': {'children': [{'10A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'},
'H05VVF2x1.00mm': {'children': [{'10A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'},
'H05VVF3G0.75mm': {'children': [{'2.5A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'},
'H05VVF3G1.0mm': {'children': [{'10A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'},
'H05VVF3G1.5mm': {'children': [{'16A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Cables'}}],
'name': 'Products'},
'ZA-Plug': {'children': [{'SABS-164/1:1992': {'children': [{'16A/250V': {'children': [{}],
'name': 'Max-Rating'}}],
'name': 'Plug'}}],
'name': 'Products'}}],
'name': 'Country'}}],
'name': 'Africa'},
Upvotes: 0
Views: 1129
Reputation: 11597
There are several problems here. The root cause of your issue is a problem with the JSON you are passing to d3.js: it is incorrectly formed. I suggest using JSONLint to verify that data/output.json
is in a correct format: http://jsonlint.com/.
In addition, you should check for parsing errors in the callback function when you read in the data. For instance, try writing something like this:
d3.json("data/output.json", function(error, json) {
if (error) {
console.log(error);
alert(error); // I would suggest a more sophisticated UI that just
// an alert box, but this suffices as an example.
}
else {
root = json[0];
update(root);
}
});
Upvotes: 1