cant update json stored in Myjson.com

2.5k views Asked by At

I have stored a json in Myjson.com and I am using it in my android app. now I want to upgrade stored data but I am unable to do that. I've read the example given in http://myjson.com/api to update Json example is this :

$.ajax({
    url:"https://api.myjson.com/bins/:id",
    type:"PUT",
    data:'{"key_updated":"value_updated"}',
    contentType:"application/json; charset=utf-8",
    dataType:"json",
    success: function(data, textStatus, jqXHR){

    }
});     `

but cant figure it out. my json data is as below:

[{"id":"priceup","value":"0"},{"id":"appup","value":"0"},{"id":"nezam","value":"16000"},... ]

anybody knows how can I update above data any help will be appreciated.

1

There are 1 answers

5
gaetanoM On BEST ANSWER

When you create a new JSON the PUT method returns in the data the url of the newly created resource. Hence, you need to use this url and not the generic https://api.myjson.com/bins/:id.

An example (jsfiddle here):

function updateJson(url) {
    var obj = [{"id":"priceup","value":"0"},{"id":"appup","value":"0"},{"id":"nezam","value":"16000"}];
    var data = JSON.stringify(obj);
    $.ajax({
        url:url,
        type:"PUT",
        data: data,
        contentType:"application/json; charset=utf-8",
        dataType:"json",
        success: function(data, textStatus, jqXHR){
            console.log(data); // now data contains the updated data
        }
    });
}

//
// create a new JSON....
//
$.ajax({
    url:"https://api.myjson.com/bins",
    type:"POST",
    data:'{"key":"value"}',
    contentType:"application/json; charset=utf-8",
    dataType:"json",
    success: function(data, textStatus, jqXHR){
        var url = data.uri; // data contains the url....
        updateJson(url);
    }
});