Remove Specific JSON Keys at Once, Recursively at Any Depth

PublishedToolJSON Key Remover

Contents
  1. When you need to remove several keys at once, several levels deep
  2. How to remove keys yourself without a tool
  3. Common reasons the result doesn't match what you expected
  4. Summary
  5. Frequently asked questions

If you need to remove several sensitive properties at once — password, token, hash — from a nested JSON response or log data, you have to find and remove every one of them, whether the key sits at the top level or several layers down inside an object within an array.

JSON Key Remover takes a comma-separated list of key names, walks the whole object tree recursively, and drops only the properties whose names match exactly, copying everything else as-is.

This post lays out, fact by fact, the rules that govern how that matching and traversal work internally, and how to read the values shown in the results pane.

Summary ① Keys separated by commas are each trimmed and then matched at the same time, and the walk covers everything recursively, including objects inside arrays and deeply nested objects. ② When the Ignore case option is on, key names are lowercased before comparison; when it's off, only an exact match on the original text is removed. ③ Matching is exact-match only, so substrings are not removed, and if the JSON is malformed you get an error and an empty output pane.

When you need to remove several keys at once, several levels deep

When you log an API response or attach it to a bug report, it often carries a mix of fields you shouldn't expose as-is, such as password, secret_token, and salt. The problem is that these fields aren't only in one top-level object.

On a user list API response, every object inside the users array has a password, and each of those objects also has a session sub-object where secret_token shows up again.

Deleting these one by one by eye in a text editor multiplies the work by the number of array elements, and it's easy to miss one.

What you want is a single rule — "remove every key with this name, wherever it lives" — applied consistently across the whole object tree.

How to remove keys yourself without a tool

You can do this with a few lines of code in any JavaScript runtime (a browser devtools console or Node.js). The key piece is the replacer function that JSON.stringify takes as its second argument.

Walking the tree recursively with a replacer function

In JSON.stringify(value, replacer, space), when replacer is a function, it's called recursively for every property of every object and array, starting from the top-level value. Each call receives the current key name and value.

Returning undefined drops that property from the resulting string entirely. The same recursion applies to objects inside arrays, so it works at any nesting depth without you writing separate traversal code.

Write the key names you want to remove as a comma-separated array, trim each entry, and compare inside the replacer.

const raw = JSON.parse(inputText);
const targetKeys = "password, secret_token, salt"
  .split(",")
  .map((k) => k.trim());

const cleaned = JSON.stringify(
  raw,
  (key, value) => (targetKeys.includes(key) ? undefined : value),
  2
);

Removing keys regardless of case

If you want case-insensitive removal, lowercase both the key names and the list before comparing.

const lowerKeys = targetKeys.map((k) => k.toLowerCase());
const cleaned = JSON.stringify(
  raw,
  (key, value) => (lowerKeys.includes(key.toLowerCase()) ? undefined : value),
  2
);

This code is exactly what the tool does internally. But instead of opening the console and pasting code every time, you can paste your JSON into the input box, change the key names, and see the result right away with the tool below.

Try it with our free tool.

JSON Key Remover

Runs entirely in your browser with no server upload. List multiple keys separated by commas and they're removed recursively.

Common reasons the result doesn't match what you expected

When the output makes you ask "why wasn't this key removed" or "why did this part get removed too," it's usually one of these four things.

SituationWhat actually happens
You listed only password but the data also has password_hash and user_passwordMatching is exact only, so they aren't removed. This is not substring matching.
You put a space after the comma or you don't (password, token vs password,token)Each key name is trimmed after splitting, so the spacing makes no difference to the result.
The Ignore case checkbox is on (the default)Password, PASSWORD, and password are all lowercased for comparison, so all of them are removed.
You turn the Ignore case checkbox offOnly keys with exactly the same case as the spelling in your list are removed, so Password stays.

How arrays and invalid JSON are handled

Structures with multiple objects inside an array are handled with no exceptions. The array passes each element back into the recursive function, so every target key inside every object in the array is removed, and if an object contains another object, the walk goes down into that too.

If the text in the input box isn't valid JSON, JSON.parse fails, and the screen shows only a parsing error message with nothing in the output pane.

If the result is empty, check your input JSON's syntax (missing quotes, trailing commas, and so on) before you check the key names.

Finally, the result is recalculated only when you press the button. While you're editing the input or the key list, the output doesn't update on its own; press the Remove keys button again to refresh the result from your latest input.

Summary

  • The key list is comma-separated, and each entry is trimmed automatically and then matched at the same time.
  • The recursive walk applies with no exceptions to objects inside arrays and to deeply nested objects.
  • Whether Password and password count as the same key depends on whether the Ignore case option is on or off.
  • Matching is exact only, so keys that merely contain a substring are not removed.
  • Pasting invalid JSON shows only an error and leaves the output empty, and the result is recalculated only when you press the button.
JSON Key Remover

Runs entirely in your browser with no server upload. List multiple keys separated by commas and they're removed recursively.

Frequently asked questions

Are keys removed if only part of the name matches?

No. Matching checks only for an exact match against the trimmed key name, so if you list just token, only the key token is removed, while keys that match only part of the name, like access_token or token_id, stay as they are.

Can I write the keys without spaces after the commas?

Yes. The key list is split on commas and each entry is trimmed, so password,token and password, token produce the same matches.

Are keys in multiple objects inside an array removed all at once?

Yes. Each element of the array is walked recursively, so if 100 objects in an array all have a key with the same name, it's removed from all 100. If those objects contain other objects or arrays, the walk keeps going down.

What happens if the JSON I paste is malformed?

Processing stops at the point where JSON.parse fails. The screen shows only a parsing error message, and nothing is filled into the output pane. Missing quotes and an unnecessary comma after the last item (a trailing comma) are the usual causes.

This article was adapted from our Korean original. Read the Korean original

Related articles

  1. JSON Key Sorting: What It Actually ChangesJSON key sorting reorders object keys alphabetically and leaves arrays alone. See how recursion and indent settings change the output, with examples.
  2. JS Object to JSON: Why JSON.parse Fails and How to Fix ItWhy a JS object literal breaks JSON.parse, how it differs from JSON syntax rules, and how to convert it in the browser console without any tools.
  3. JSON Schema Generator: How Types, required, and anyOf WorkSee how a JSON sample becomes a Draft-07 schema: how string, number, and array types get picked, what required and anyOf mean, and where the result stops.
  4. JSON Syntax Error: How to Read the Message and Find the CauseLearn what the position value in a JSON error message means and how to spot trailing commas, single quotes, comments, and other common syntax mistakes.
  5. JSON to TypeScript: Optional (?) and Union (|) Types ExplainedSee how JSON-to-TypeScript conversion decides which keys get a question mark and which values become union types, using a real JSON sample.
  6. JSON to YAML Conversion: Arrays, Indentation, and Quote RulesSee how JSON arrays turn into dash lists and nested objects into indented levels, how 2- vs 4-space indent works, and the quoting mistakes to avoid.

Related tools