I have a JSON data and I want to delete all the items in the array and I have already tried delete, Products.length = 0 but nothing works. I am able to add a new item, modify and delete a particular one but unable to remove all.
The JSON Data
{
"Products":[
{
"productID":"75",
"productName":"Mango Juice",
"productQuantity":3,
"seller_1":"30.00",
"seller_2":"40.00",
"seller_3":"34.50"
},
{
"productID":"25",
"productName":"Apple Juice",
"productQuantity":1,
"seller_1":"70.00",
"seller_2":"74.00",
"seller_3":"84.50"
},
{
"productID":"10",
"productName":"Orange Juice",
"productQuantity":1,
"seller_1":"10.00",
"seller_2":"20.00",
"seller_3":"12.50"
}
]
}
After deleting all the items, I should get this as final
{"Products": []}
Here is the Jquery process
$(document).ready(function() {
$.getJSON( "data.json", function(data) {
data.Products = [];
$.ajax({
type: "POST",
url: "json.php",
dataType: 'json',
data: { json: data }
});
});
});
The Php process
if(isset($_POST['json'])) {
$fp = fopen('data.json', 'w+');
fwrite($fp, json_encode($_POST['json']));
fclose($fp);
}
console.log(data); shows that the code has been well executed on javascript side but it does not update the JSON via PHP