1

hello friends i want to rename my JSON key so below i my code

 var json = '[{"Status":"Success","Data":[{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"31","WishListName":"General","WishListItems":[{"WishListItemID":"3","ItemCode":"4414082000005","ItemName":"Notebook 4414-082","Image":"http://aljeel.gct.om/ProductsImages/4414082000005_MT1.jpg","ItemPrice":0.500,"CreatedDate":"25/05/2018"}]},{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"24","WishListName":"Default","WishListItems":[]}],"Message":null}]';

var obj = JSON.parse(json)[0].Data;
obj.data = obj.WishListItems;
delete obj.WishListItems;

json = JSON.stringify([obj]);
console.log("FINAL JSON " + (json));

i want to change WatchListItem key with data key but when i run above code JSON key is not replacing any idea how can i solve this?

Harshal Kalavadiya
  • 2,412
  • 4
  • 38
  • 71
  • 1
    Data key is also an array so you may need to do `var obj = JSON.parse(json)[0].Data[0];` instead – Babis.amas Jun 05 '18 at 11:50
  • Does this answer your question? [How to rename JSON key](https://stackoverflow.com/questions/13391579/how-to-rename-json-key) – bsplosion Mar 17 '22 at 13:40

3 Answers3

4

Try this :

var json = '[{"Status":"Success","Data":[{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"31","WishListName":"General","WishListItems":[{"WishListItemID":"3","ItemCode":"4414082000005","ItemName":"Notebook 4414-082","Image":"http://aljeel.gct.om/ProductsImages/4414082000005_MT1.jpg","ItemPrice":0.500,"CreatedDate":"25/05/2018"}]},{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"24","WishListName":"Default","WishListItems":[]}],"Message":null}]';

var obj = JSON.parse(json)[0].Data;

console.log("Before replace", obj);

var res = obj.map(item => {
  item.data = item.WishListItems;
  delete item.WishListItems;
  return item;
});

console.log("After replace", res);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
1

obj was undefined. Changing obj to obj[0] fixed it.

var json = '[{"Status":"Success","Data":[{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"31","WishListName":"General","WishListItems":[{"WishListItemID":"3","ItemCode":"4414082000005","ItemName":"Notebook 4414-082","Image":"http://aljeel.gct.om/ProductsImages/4414082000005_MT1.jpg","ItemPrice":0.500,"CreatedDate":"25/05/2018"}]},{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"24","WishListName":"Default","WishListItems":[]}],"Message":null}]';

var obj = JSON.parse(json)[0].Data[0];

obj.data = obj.WishListItems;
delete obj.WishListItems;

json = JSON.stringify([obj]);
console.log("FINAL JSON " + (json));
Red
  • 6,599
  • 9
  • 43
  • 85
  • with your code it remove inside WishListitem data , it is not replace WishListItems with data key – Harshal Kalavadiya Jun 05 '18 at 11:59
  • @HarshalKalavadiya firstly he creates a new node with the name 'data' where he assignes the value from the 'WishListItems' node and then he deletes the second node, so you will have a key named data with the 'WishListItems' content. Did you try it? – Babis.amas Jun 05 '18 at 12:06
  • 1
    Your question is very unclear. I have no idea what you are trying to archieve. – Red Jun 05 '18 at 12:07
  • @Red My question is clear i want to rename WishListItems key with data key, i already highlight those key in my question – Harshal Kalavadiya Jun 05 '18 at 12:10
  • Babis.amas: i tried above code but it is remove inside WishListItems array data i want to just rename WishListItems with data key – Harshal Kalavadiya Jun 05 '18 at 12:16
  • @Red you have to assign that list to `obj[0].data` – barbsan Jun 05 '18 at 12:17
0

The issue with your code is that your obj is not one object it is an array. you have to use

var obj = JSON.parse(json)[0].Data[0]; 

in the assignement. Or to do it for all objects

use forEach like below

    var json = '[{"Status":"Success","Data":[{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"31","WishListName":"General","WishListItems":[{"WishListItemID":"3","ItemCode":"4414082000005","ItemName":"Notebook 4414-082","Image":"http://aljeel.gct.om/ProductsImages/4414082000005_MT1.jpg","ItemPrice":0.500,"CreatedDate":"25/05/2018"}]},{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"24","WishListName":"Default","WishListItems":[]}],"Message":null}]';

JSON.parse(json)[0].Data.forEach(obj=>{
obj.data = obj.WishListItems;
    delete obj.WishListItems;
 json = JSON.stringify([obj]);
    console.log("FINAL JSON " + (json));
});
Jins Peter
  • 2,368
  • 1
  • 18
  • 37