1

For example I receive a JSON response from postman and store it in a variable. How can I remove a specific key from this response?

In my example, I want a solution that removes head_out_timestam - even if the exact hierarchy of the object tree is unknown.

I need this in javaScript, thanks.

{
"soapenv:Envelope": {
    "$": {
        "xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/"
    },
    "soapenv:Header": {
        "f:Routing": {
            "f:Receiver": {
                "f:Application": "Something"
            }
        }
    },
    "soapenv:Body": {
        "Something": {
            "something_output": {
                "service_header_out": {
                    "head_out_servercode": "Some",
                    "head_out_timestam": "2019-06-18-11.32.13.570127",
                }
            }
        }
    }
}

}

Henke
  • 4,445
  • 3
  • 31
  • 44
  • `delete parsedResponse['sopaenv:Envelope']['soapenv:Body']['Something']['something_output']['service_header_out']['head_out_timestam']`.. Why that, though? If that's coming from the server, can't you simply ignore it? – briosheje Jun 18 '19 at 09:55
  • Since you're searching for a JavaScript solution I assume you're handling a JavaScript object rather than its JSON-serialized form. Then all you need is the `delete` operator : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete – Aaron Jun 18 '19 at 09:57
  • It was just some example data. But lets say, I only have the info, that "head_out_timestam" should be removed. Is there an easy way to archieve it? – Josipa Marija Jun 18 '19 at 09:58
  • @Aaron its an nested object, which can not so easily delete a property – Josipa Marija Jun 18 '19 at 09:58
  • Why? `delete outerObject.innerObject.property` – Aaron Jun 18 '19 at 10:00
  • @Aaron yes, but its only an example of a data which I archieve. Imagine, if its more nested like yet and I dont know, how much nested it is. Is there a way, just to remove it based on the key and without the knowing of the inner objects? – Josipa Marija Jun 18 '19 at 10:03
  • you would likely have to check the key , if its not matching to 'head_out_timestam' then get the value and traverse through those , till u find 'head_out_timestam', BFS or DFS would be the algo you need to implement – nivendha Jun 18 '19 at 10:04
  • Ah, so you need recursive object transversal. [This question](https://stackoverflow.com/questions/2549320/looping-through-an-object-tree-recursively) explains it quite well, you just need to add a delete when you encounter a `head_out_timestam` key. – Aaron Jun 18 '19 at 10:24
  • @JosipaMarija, you can check the solution I posted in the answer. – Radonirina Maminiaina Jun 18 '19 at 10:28

4 Answers4

1

You can do a recursive search of the key into your object then delete the found key.

Check the following solution:

var json = {
"soapenv:Envelope": {
    "$": {
        "xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/"
    },
    "soapenv:Header": {
        "f:Routing": {
            "f:Receiver": {
                "f:Application": "Something"
            }
        }
    },
    "soapenv:Body": {
        "Something": {
            "something_output": {
                "service_header_out": {
                    "head_out_servercode": "Some",
                    "head_out_timestam": "2019-06-18-11.32.13.570127",
                }
            }
        }
    }
}
}

var removeKey = (json, key) => {
  Object.keys(json).forEach((item) => {
   if (item !== key) {
      // make sure json[item] is an object
     if (typeof json[item] === 'object') {
       removeKey(json[item], key)
      }
    } else {
     delete json[item];
    }
  })
  return json;
}
console.log(removeKey(json, 'head_out_timestam'))

Note: you can use Object.keys(obj).forEach or for ... in

Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60
0

var response = {
    "soapenv:Envelope": {
        "$": {
            "xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/"
        },
        "soapenv:Header": {
            "f:Routing": {
                "f:Receiver": {
                    "f:Application": "Something"
                }
            }
        },
        "soapenv:Body": {
            "Something": {
                "something_output": {
                    "service_header_out": {
                        "head_out_servercode": "Some",
                        "head_out_timestam": "2019-06-18-11.32.13.570127",
                    }
                }
            }
        }
    }
}

console.log(response["soapenv:Envelope"]["soapenv:Body"]["Something"]["something_output"]["service_header_out"]["head_out_timestam"]);

delete response["soapenv:Envelope"]["soapenv:Body"]["Something"]["something_output"]["service_header_out"]["head_out_timestam"]
console.log(response["soapenv:Envelope"]["soapenv:Body"]["Something"]["something_output"]["service_header_out"]["head_out_timestam"]);
Ziv Ben-Or
  • 1,149
  • 6
  • 15
0

For deleting head_out_timestam

Just try this way

 let response={
    "soapenv:Envelope": {
        "$": {
            "xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/"
        },
        "soapenv:Header": {
            "f:Routing": {
                "f:Receiver": {
                    "f:Application": "Something"
                }
            }
        },
        "soapenv:Body": {
            "Something": {
                "something_output": {
                    "service_header_out": {
                        "head_out_servercode": "Some",
                        "head_out_timestam": "2019-06-18-11.32.13.570127"
                    }
                }
            }
        }
    }
    }
    //for deleting the required key-value 
 

delete response["soapenv:Envelope"]["soapenv:Body"]["Something"]["something_output"]["service_header_out"]["head_out_timestam"]
    
    //Check the new response 
    console.log(response);
Alex Varghese
  • 2,000
  • 16
  • 17
0

You can use the delete operator in javascript according to your example you can simply use,

delete response[""soapenv:Envelope""][""soapenv:Body""].Something.something_output.service_header_out.head_out_timestam;