392

if I have a JSON object say:

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

can I remove 'key1' so it becomes:

{'test' : {'key2': 'value'}}
Josef Pfleger
  • 74,165
  • 16
  • 97
  • 99
g00se0ne
  • 4,560
  • 2
  • 21
  • 14
  • 16
    Just to be pedantic, that's a Javascript object, not a "JSON object" JSON is the string representation of Javascript object. – Davy8 Mar 31 '12 at 14:30

2 Answers2

644

Simple:

delete myObj.test.key1;
Josef Pfleger
  • 74,165
  • 16
  • 97
  • 99
  • 27
    delete myObj.test['key1']; would work as well. – MyItchyChin Aug 02 '09 at 19:40
  • 39
    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. – Sinan Taifour Aug 02 '09 at 20:21
  • 2
    This is the best JavaScript keyword I have ever seen ! – Sandip Subedi Aug 31 '17 at 19:33
  • 2
    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. –  Nov 20 '17 at 01:27
  • `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. –  Nov 20 '17 at 01:36
  • Works well in my code – Ajay Kumar Jun 27 '18 at 04:20
  • Is it possible to soft delete the item so that it will still be in DOM but considered for specific functionality? – Shardul Jan 16 '19 at 10:43
  • Is there a good way that I could do this to a large file? – Adam Patterson Oct 01 '20 at 22:50
133

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.

Neji Soltani
  • 1,522
  • 4
  • 22
  • 41
praneetloke
  • 1,953
  • 1
  • 14
  • 15
  • 1
    i like the option for dynamism. it helped in my own case to remove a property dynamically from a json object – user1862764 May 19 '16 at 12:19
  • 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 – zhihong Apr 12 '17 at 12:05
  • 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 –  Oct 31 '17 at 13:52
  • @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 https://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. – praneetloke Oct 31 '17 at 18:29
  • What if you don't know where in the json the key appears? – ggb667 Mar 30 '18 at 13:02
  • 1
    @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. – praneetloke Apr 01 '18 at 17:16