Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove json object key and value.?

I have a json object as shown below. where i want to delete the "otherIndustry" entry and its value by using below code which doesn't worked.

var updatedjsonobj = delete myjsonobj['otherIndustry'];

How to remove Json object specific key and its value. Below is my example json object where i want to remove "otherIndustry" key and its value.

var myjsonobj =  {
      "employeeid": "160915848",
      "firstName": "tet",
      "lastName": "test",
      "email": "[email protected]",
      "country": "Brasil",
      "currentIndustry": "aaaaaaaaaaaaa",
      "otherIndustry": "aaaaaaaaaaaaa",
      "currentOrganization": "test",
      "salary": "1234567"
    };
delete myjsonobj ['otherIndustry'];
console.log(myjsonobj);

where the log still prints the same object without removing 'otherIndustry' entry from the object.

like image 982
krish kr Avatar asked Oct 06 '17 06:10

krish kr


People also ask

How do you remove a key-value pair from a JSON object in Java?

JsonObject::remove() removes a key-value pair from the object pointed by the JsonObject . If the JsonObject is null, this function does nothing.

How do you remove an element from a JSON object?

To remove item from json object in javascript, use delete keyword it will remove key value pair from object only you have to mantion delete keyword with key.

How do you remove a key-value pair from a JSON array?

Remove Key Value Pair from JSON: In order to remove an attribute from json you have to use JS delete method. This will delete the json attribute with the specific key. Likewise you can remove the occurrence of particular attribute from json array.

How do you remove a JSON object in Python?

To delete a JSON object from a list: Parse the JSON object into a Python list of dictionaries. Use the enumerate() function to iterate over the iterate over the list. Check if each dictionary is the one you want to remove and use the pop() method to remove the matching dict.


5 Answers

delete operator is used to remove an object property.

delete operator does not returns the new object, only returns a boolean: true or false.

In the other hand, after interpreter executes var updatedjsonobj = delete myjsonobj['otherIndustry']; , updatedjsonobj variable will store a boolean value.

How to remove Json object specific key and its value ?

You just need to know the property name in order to delete it from the object's properties.

delete myjsonobj['otherIndustry'];

let myjsonobj = {
  "employeeid": "160915848",
  "firstName": "tet",
  "lastName": "test",
  "email": "[email protected]",
  "country": "Brasil",
  "currentIndustry": "aaaaaaaaaaaaa",
  "otherIndustry": "aaaaaaaaaaaaa",
  "currentOrganization": "test",
  "salary": "1234567"
}
delete myjsonobj['otherIndustry'];
console.log(myjsonobj);

If you want to remove a key when you know the value you can use Object.keys function which returns an array of a given object's own enumerable properties.

let value="test";
let myjsonobj = {
      "employeeid": "160915848",
      "firstName": "tet",
      "lastName": "test",
      "email": "[email protected]",
      "country": "Brasil",
      "currentIndustry": "aaaaaaaaaaaaa",
      "otherIndustry": "aaaaaaaaaaaaa",
      "currentOrganization": "test",
      "salary": "1234567"
}
Object.keys(myjsonobj).forEach(function(key){
  if (myjsonobj[key] === value) {
    delete myjsonobj[key];
  }
});
console.log(myjsonobj);
like image 115
Mihai Alexandru-Ionut Avatar answered Sep 30 '22 13:09

Mihai Alexandru-Ionut


There are several ways to do this, lets see them one by one:

  1. delete method: The most common way

const myObject = {
    "employeeid": "160915848",
    "firstName": "tet",
    "lastName": "test",
    "email": "[email protected]",
    "country": "Brasil",
    "currentIndustry": "aaaaaaaaaaaaa",
    "otherIndustry": "aaaaaaaaaaaaa",
    "currentOrganization": "test",
    "salary": "1234567"
};

delete myObject['currentIndustry'];
// OR delete myObject.currentIndustry;
  
console.log(myObject);
  1. By making key value undefined: Alternate & a faster way:

let myObject = {
    "employeeid": "160915848",
    "firstName": "tet",
    "lastName": "test",
    "email": "[email protected]",
    "country": "Brasil",
    "currentIndustry": "aaaaaaaaaaaaa",
    "otherIndustry": "aaaaaaaaaaaaa",
    "currentOrganization": "test",
    "salary": "1234567"
  };

myObject.currentIndustry = undefined;
myObject = JSON.parse(JSON.stringify(myObject));

console.log(myObject);
  1. With es6 spread Operator:

const myObject = {
    "employeeid": "160915848",
    "firstName": "tet",
    "lastName": "test",
    "email": "[email protected]",
    "country": "Brasil",
    "currentIndustry": "aaaaaaaaaaaaa",
    "otherIndustry": "aaaaaaaaaaaaa",
    "currentOrganization": "test",
    "salary": "1234567"
};


const {currentIndustry, ...filteredObject} = myObject;
console.log(filteredObject);

Or if you can use omit() of underscore js library:

const filteredObject = _.omit(currentIndustry, 'myObject');
console.log(filteredObject);

When to use what??

If you don't wanna create a new filtered object, simply go for either option 1 or 2. Make sure you define your object with let while going with the second option as we are overriding the values. Or else you can use any of them.

hope this helps :)

like image 33
Mohammed Amir Ansari Avatar answered Sep 30 '22 11:09

Mohammed Amir Ansari


Follow this, it can be like what you are looking:

var obj = {
    Objone: 'one',
    Objtwo: 'two'
};

var key = "Objone";
delete obj[key];
console.log(obj); // prints { "objtwo": two}
like image 45
Muayyad Ayesh Avatar answered Sep 30 '22 11:09

Muayyad Ayesh


Here is one more example. (check the reference)

const myObject = {
  "employeeid": "160915848",
  "firstName": "tet",
  "lastName": "test",
  "email": "[email protected]",
  "country": "Brasil",
  "currentIndustry": "aaaaaaaaaaaaa",
  "otherIndustry": "aaaaaaaaaaaaa",
  "currentOrganization": "test",
  "salary": "1234567"
};
const {otherIndustry, ...otherIndustry2} = myObject;
console.log(otherIndustry2);
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}
like image 27
Penny Liu Avatar answered Sep 30 '22 12:09

Penny Liu


function omit(obj, key) {
    const {[key]:ignore, ...rest} = obj;
    return rest;
}

You can use ES6 spread operators like this. And to remove your key simply call

const newJson = omit(myjsonobj, "otherIndustry");

Its always better if you maintain pure function when you deal with type=object in javascript.

like image 27
sachinkondana Avatar answered Sep 30 '22 11:09

sachinkondana