I have product.json
, admin.html
and a admin.js
files.
My goal is to add new products in JSON through an admin page, JSON is used in my website to show a list of products.
NOTE: I only want to add objects in json from client side, I've made a e-commerce website which lists all the product and a admin panel login to add more products, so nothing is serverside and will be done locally.
The JSON contains an array of objects, like:
[
{
"company": "xyz",
"item": "shirt",
"price": 100,
"discount": 10%
}
]
I have to add more objects in JSON through an admin page which contains a html form, like:
<form>
<h3>Company</h3>
<input type="text" id="campany" placeholder="enter company name">
<h3>Item</h3>
<input type="text" id="item" placeholder="enter item name">
<h3>Price</h3>
<input type="text" id="price" placeholder="enter price">
<h3>Discount</h3>
<input type="text" id="discount" placeholder="enter disc.">
</form>
<button onclick="show()" class="btn">Submit</button>
The js file is like:
const obj = fetch("json/products.json")
var myJSON = JSON.stringify(obj);
var addObj;
function show()
{
addObj =
`{"campany": "${campany.value}",
"item": "${item.value}",
"price": ${price.value},
"discount": "${discount.value}%"},`
myJSON = myJSON + addObj;
console.log(obj);
}
obj.push(addObj);
I'm new to js and dealing with JSON for the first time, all the other related answers are not helping me and I can't find the right things in the docs. I hope I'm clear with my goal, please help me.