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?

2
  • 1
    Data key is also an array so you may need to do var obj = JSON.parse(json)[0].Data[0]; instead Commented Jun 5, 2018 at 11:50
  • Does this answer your question? How to rename JSON key Commented Mar 17, 2022 at 13:40

3 Answers 3

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);

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

Comments

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));

6 Comments

with your code it remove inside WishListitem data , it is not replace WishListItems with data key
@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?
Your question is very unclear. I have no idea what you are trying to archieve.
@Red My question is clear i want to rename WishListItems key with data key, i already highlight those key in my question
Babis.amas: i tried above code but it is remove inside WishListItems array data i want to just rename WishListItems with data key
|
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));
});

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.