0

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

2
  • If you have JSON you have a string - so make up your mind: do you have a JSON STRING or do you have an Object with an Array inside under hash key "Products"? Obviously changing a (JSON) string requires a very different approach from working with Object/Array structures. If you have an Array and not a string - see here: stackoverflow.com/questions/500606/… Commented Dec 7, 2013 at 20:24
  • Actually - see the bottom-most answer for this question, with 1186 upvotes, 5 times more than the accepted answer: stackoverflow.com/questions/1232040/… Commented Dec 7, 2013 at 20:35

2 Answers 2

2

If you have this:

var 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"
      }
   ]
}

Then, you can just do:

data.Products = [];

That will just set data.Products to be a newly created empty array.

If you have any reason to want to keep the same array in place (e.g. references you want to now be pointing to the empty array), you can also do:

data.Products.length = 0;

Note: there are many other ways to do this also including splicing all the elements out, popping all the elements out, etc... Modifying the original array by removing its items rather than assigning a new empty array will modify the array that any outside references might also be pointing to (which you may or may not want depending upon the circumstances).


FYI, JSON is a text format. What you have here is a javascript object. The two are not the same.

Sign up to request clarification or add additional context in comments.

7 Comments

That's what I would do but I just found this and there is overwhelming support for setting array.length=0 (the 1000+ upvotes answer is at the bottom!!!): stackoverflow.com/questions/1232040/… Hmmm....
@Mörre - There is no discussion of anyone needing to keep references intact to the original array which is the main advantage of setting .length.
It does not look to me like an option that one should only choose in that special situation - besides, if you always do it that way you don't need to check if you have any other references, which you will have to keep in mind if you do it in any other way. So knowing we are human from now on I will go with setting length to 0, because I need my limited brain power for other things than checking if all my arrays may have any additional references pointing to them :) One should use the option that ALWAYS works other one where you have to think.
@Mörre - I added some info to my answer about other ways to do it, but absent any other requirements (performance, references, etc...) and the OP's question about just looking for a way to do it, I don't think we need to go into all the possible ways to do it and all the possible pros/cons. I'm trying to not make this more complicated than was asked for.
I have and had no intention to have a discussion with you - but for the record and the benefit of OP I wanted to have the link to that other Q here. I think that's fair to all. And by the way that upvote was/is mine.
|
0

You can try using the splice function

var arr =[1,2,3,4];
arr.splice(0,arr.length);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.