views:

28

answers:

3

I've a large JSON string describing some data. I'd like to loop through this and replace value of all properties named "Key". How can I achieve this using jQuery or plain Javascript?

+1  A: 
for ( var i in data ) {
    for ( var k in data[i] ) {
        if ( k == 'Key' ) {
            data[i][k] = 'new value';
        }
    }
}
hsz
+1  A: 
var item = $.parseJson("jsonstring...");
var target = "Key";
for (var k in item) {
  if (item.hasOwnProperty(k) && item[k].hasOwnProperty(target)) {
    item[k][target] = "replacement value"
  }
} 

Or similar, depending on the structure of your JSON. This is assuming it's a list of objects.

jeremiahd
A: 

for (property in setJson)

{ if (property != 'Key') //do some think }

Haroldis