I'm trying to export JSON file data to a table but I'm not sure how to access nested elements of the JSON file.
This is my JSON:
{
"fruits color": "red",
"farmers market": "2019",
"sellers": [{
"day": "1",
"seller": [{
"name": "John",
"sales": "50",
"types": "3",
"type": [
"apples",
"strawberries",
"rapsberries"
]
},
{
"name": "Adam",
"sales": "45",
"types": "2",
"type": [
"cranberries",
"redcurrants"
]
}
]
}
]}
I have a simple HTML form where I can select the file from a drop-down menu.
When my file is selected my JavaScript function pulls the information from it and sends it to a table:
function fruitSelection() {
var items = document.getElementById("fruit-colors");
var value = items.options[items.selectedIndex].value;
if (value == "red") {
JSON = "redfruits.json"
processFruitsFile()}}
function processFruitsFile() {
$.getJSON(JSON, function(JSONdata) {
var JSONItems= [];
$.each(JSONdata, function(key, value) {
JSONItems.push( "<tr" + key + "'>" + value + "</tr>");
});
$( "<tr/>", {
"class": "result-items",
html: JSONItems.join("")
}).appendTo(result);});}
Now the problem I'm having is that my function works only with the first key and value pair like this:
red2019[object Object]
What I'm trying to achieve is a table where I can display something like this:
Day Seller Type 1 Type 2 Type 3
1 John apples strawberries raspberries
How can I access those values that are nested inside 'seller' and 'type of fruits' and format them nicely?
Thanks for help!
Type 1, Type 2....in main header of table?