0

I have a json file that looks like this:

{
 "@id": "2",
 "@samsid": "d7058536c89b46c0b58117eff64372f7",
 "@productname": "Another paid for",
 "@downloaddate": "2013-05-28 11:37:12Z",
 "@downloadlocation":
    "2AFoGZVJpFHzspQD9UoE2C4VFYc8idiO4ebaRx4uEvtG+79DjkOUjJQjp9lJdSk54KIbFqzr",
 "@upgradeavailable": "2wECEpVJpFHS",
 "@downlaodstatus": "3QGf15VJpFFrUzr6oxwoew=="
}

I want to update the value of the attribute @id eg (@id:2 To @id:4).

How would I do this?

7
  • 1
    Do you want to update the actual file? Commented May 28, 2013 at 6:23
  • Yes, I want to update the actual file. Commented May 28, 2013 at 6:24
  • On the server or on the client? Commented May 28, 2013 at 6:25
  • You would have to do that server-side. Commented May 28, 2013 at 6:25
  • 1
    @vipinkatiyar You mean you have a .json file you want to update? Then your question is not a javascript problem! You'll have to use another language, most probably your backend server language. Anyway I updated my answer for this. Commented May 28, 2013 at 6:27

2 Answers 2

3

Update : Since you now mentioned you want to sore this into a file! You could still set the json into a variable like below, then set it's value. Then use JSON.stringify() to convert it into a string and then write it into your file on the backend. This would really depend on how your system is designed.


I'm not sure why but since you have @ in the keys for your JSON object you will have to get and set them like this:

//assign into a variable
var x = {
"@id": "2",
"@samsid": "d7058536c89b46c0b58117eff64372f7",
"@productname": "Another paid for",
"@downloaddate": "2013-05-28 11:37:12Z",
"@downloadlocation":
   "2AFoGZVJpFHzspQD9UoE2C4VFYc8idiO4ebaRx4uEvtG+79DjkOUjJQjp9lJdSk54KIbFqzr",
"@upgradeavailable": "2wECEpVJpFHS",
"@downlaodstatus": "3QGf15VJpFFrUzr6oxwoew=="
};
alert(x["@id"]); //shows 2
x["@id"] = 4
alert(x["@id"]);//shows 4

See a working fiddle : http://jsfiddle.net/jPa6n/

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

Comments

0

if you use javascript and some libraries try something like this (I use jQuery as it is popular)

var a = JSON.parse('{"@id": "2","@samsid": "d7058536c89b46c0b58117eff64372f7","@productname": "Another paid for","@downloaddate": "2013-05-28 11:37:12Z","@downloadlocation": "2AFoGZVJpFHzspQD9UoE2C4VFYc8idiO4ebaRx4uEvtG+79DjkOUjJQjp9lJdSk54KIbFqzr","@upgradeavailable": "2wECEpVJpFHS","@downlaodstatus": "3QGf15VJpFFrUzr6oxwoew=="}');
a['@id'] = 4;
var b = JSON.stringify(a);

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.