399

if I have a JSON object say:

var myObj = {'test' : {'key1' : 'value', 'key2': 'value'}}

can I remove 'key1' so it becomes:

{'test' : {'key2': 'value'}}
2
  • 17
    Just to be pedantic, that's a Javascript object, not a "JSON object" JSON is the string representation of Javascript object. Commented Mar 31, 2012 at 14:30
  • @DavidLy Not a pedantic. Scholarly. Commented Apr 16 at 15:45

2 Answers 2

658

Simple:

delete myObj.test.key1;
Sign up to request clarification or add additional context in comments.

8 Comments

delete myObj.test['key1']; would work as well.
So would delete myObj['test']['key1']; you can interchange whatever.x and whatever['x'] as long as x is a valid variable name, so even delete myObj['test'].key1 would work.
This is the best JavaScript keyword I have ever seen !
It may be worth noting that the delete keyword mutates the Object which is frequently not exactly the intended behavior. One way to maintain immutability (for flat objects) is to make use of the Object.assign or spread operator to shallow clone prior to performing the delete.
delete Object.assign({}, myObj.test).key of course this would need to be again this is a flat clone so the key 'test' would need to be added back if that were needed for the object structure.
|
135

The selected answer would work for as long as you know the key itself that you want to delete but if it should be truly dynamic you would need to use the [] notation instead of the dot notation.

For example:

var keyToDelete = "key1";
var myObj = {"test": {"key1": "value", "key2": "value"}}

//that will not work.
delete myObj.test.keyToDelete 

instead you would need to use:

delete myObj.test[keyToDelete];

Substitute the dot notation with [] notation for those values that you want evaluated before being deleted.

6 Comments

i like the option for dynamism. it helped in my own case to remove a property dynamically from a json object
When checking the solution above, already think about what is the solution if it is a variable, thanks for saving my time for the trick
Hey @praneetloke I have one query I get JSON array Ex: [{\"Countrycode\":\"DE\",\"count\":\"3\"}] but i want to get like[{"DE":"3"}] like this but i don't get this output Please help me
@Brij your question is not related to the question on this page. Without much context about your question, I can only say that you should look at lodash.com/docs/4.17.4#map from Lodash. The map function will allow you to create a new array that has the value of the original array's CountryCode as the key in the new array, and the value of the count property from original array as the value in the new array. If this doesn't help, you should start a new question on Stackoverflow. You may also want to see if you can have the server return the response the way that you want it.
@ggb667 you don't need to. Ordering doesn't matter for a JSON object. Without getting too deep in my explanation, it's like a Dictionary (or Map). Order matters if you are dealing with an array that contains objects or other values, since they don't have a "key". Arrays use a position based index, which is why you would always access values in an array using integer based indices.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.