My goal is to generate a unique JSON dataset structured in the following way: JSON:
{
"category": {
"id": "10100",
"name": "Dessert",
"products": [
{
"id": "10101",
"name": "Cheese Cake",
"description": "New York Style",
"price": 1.55,
"price_before_discount": 1.55,
"imageURL": "image2.jpg"
},
{
"id": "10102",
"name": "Apple Pie",
"description": "Old Fashion Home Made Pie.",
"price": 1.75,
"price_before_discount": 1.75,
"imageURL": "image3.jpg"
}
],
"id": "10104",
"name": "Breakfast",
"products": [
{
"id": "10104",
"name": "Ham and Eggs",
"description": "With Hash Browns and Toast.",
"price": 4.75,
"price_before_discount": 4.75,
"imageURL": "image4.jpg"
}
]
}
}
Javascript code:
var menu = {};
var category = {}
var products = []
menu.category = category;
var catID = "10100";
var catName = "Dessert";
var catItems = {
"id": catID,
"name": catName
}
Object.assign(menu.category, catItems)
menu.category.products = products;
var itemID = "10101";
var itemName = "Cheese Cake";
var itemDescription = "New York Style"
var itemPrice = 1.55
var itemPrice_before_discount = 1.55
var itemImageURL = "image.jpg"
var items = {
"id": itemID,
"name": itemName,
"description": itemDescription,
"price": itemPrice,
"price_before_discount": itemPrice_before_discount,
"imageURL": itemImageURL
}
menu.category.products.push(items);
var itemID = "10102";
var itemName = "Apple Pie";
var itemDescription = "Old Fashion Home Made Pie."
var itemPrice = 1.75
var itemPrice_before_discount = 1.75
var itemImageURL = "image.jpg"
var items = {
"id": itemID,
"name": itemName,
"description": itemDescription,
"price": itemPrice,
"price_before_discount": itemPrice_before_discount,
"imageURL": itemImageURL
}
menu.category.products.push(items);
var cat2ID = "10102";
var cat2Name = "Breakfast";
catItems = {
"id": cat2ID,
"name": cat2Name
}
Object.assign(menu.category, catItems)
menu.category.products = products;
var itemID = "10104";
var itemName = "Ham and Eggs";
var itemDescription = "With Hash Browns and Toast"
var itemPrice = 4.75
var itemPrice_before_discount = 4.75
var itemImageURL = "image4.jpg"
var items = {
"id": itemID,
"name": itemName,
"description": itemDescription,
"price": itemPrice,
"price_before_discount": itemPrice_before_discount,
"imageURL": itemImageURL
}
menu.category.products.push(items);
console.log(JSON.stringify(menu, null, 2));
The outcome of the above code is:
{
"category": {
"id": "10102",
"name": "Breakfast",
"products": [
{
"id": "10101",
"name": "Cheese Cake",
"description": "New York Style",
"price": 1.55,
"price_before_discount": 1.55,
"imageURL": "image.jpg"
},
{
"id": "10102",
"name": "Apple Pie",
"description": "Old Fashion Home Made Pie.",
"price": 1.75,
"price_before_discount": 1.75,
"imageURL": "image.jpg"
},
{
"id": "10104",
"name": "Ham and Eggs",
"description": "With Hash Browns and Toast",
"price": 4.75,
"price_before_discount": 4.75,
"imageURL": "image4.jpg"
}
]
}
}
I am currently experiencing difficulty with adding the category node, as the data being added overrides/updates the existing category node. My query is how can I properly append the category node into its correct position.
Appreciate your help.