Reputation: 343
My API retrieve a Json like this :
{
"name": "API",
"count": 30,
"newdata": true,
"lastrunstatus": "success",
"thisversionstatus": "success",
"thisversionrun": "Mon Sep 07 2015 20:31:07 GMT+0000 (UTC)",
"results": {
"collection1": [
{
"name": "Item1",
"power": "210",
"name": {
"href": "http://picture.item42.com/item42.html",
"text": "Hammer of god"
},
"desc": "Iron Hammer",
"index": 1,
"url": "http://picture.item42.com/"
},
{
"name": "Item2",
"power": "230",
"name": {
"href": "http://picture.item42.com/item43.html",
"text": "Sword of god"
},
"desc": "Iron sword",
"index": 1,
"url": "http://picture.item43.com/"
}
]
}
I would like to delete the line "url" for each one, and delete le "href" property for the name to have "name" : "Hammer of god";
I try this and many other way (first step to delete the url): data contains the Json that I copied upper
function transform(data) {
var yourArray = data.results.collection1;
var p = 0;
var i = 0;
var destArr=[];
var srcArr=yourArray;
for(i in srcArr){
if(srcArr[i].url)
destArr[p]=srcArr[i];
p++;
}
srcArr=destArr;
return srcArr;
}
Maybe its better to use data.results.collection1.filter ? actually my code return the json without the header but still with the url row
Upvotes: 0
Views: 158
Reputation: 2117
Try the following:
jsonData.results.collection1.map(function(obj, index){
delete obj.url;
obj.name = obj.name.text;
return obj;
});
console.log(jsonData);
Upvotes: 0
Reputation: 3714
you should use the JSON delete
method for what you want to achieve:
Where data
is your json object:
data.results.collection1.forEach(function(el) {
delete el.url
delete el.name.href
});
You can simply use a second forEach loop in case you need to iterate through multiple collections.
Upvotes: 1
Reputation: 21565
Simply go through your json.result.collection1
and remove href
on each name
and url
:
for(item in json.results.collection1) {
delete json.results.collection1[item].name.href
delete json.results.collection1[item].url;
}
Upvotes: 1
Reputation: 4743
var json = document.getElementById('json').value;
var obj = JSON.parse(json);
obj.results.collection1.forEach(function(entry) {
delete entry.url;
delete entry.name.href;
});
document.getElementById('result').value = JSON.stringify(obj);
Upvotes: 1
Reputation: 301
Use the delete
keyword to delete a property from the JSON.
var jsonObj = JSON.parse('your json string');
delete jsobObj.whateveryYourpropertyName;
Upvotes: 1