JSON Filter / Pick Keys
Keep or remove specific keys from a JSON object. Supports dot notation for nested fields.
Keep Mode vs Remove Mode
Keep mode — only the keys you specify are retained in the output. Every other key is discarded. This is useful when you want to extract a subset of fields, e.g. returning only name, email, age from a large user object.
Remove mode — only the keys you specify are deleted. All other keys survive unchanged. This is useful for stripping sensitive or noisy fields before logging or sharing data, e.g. removing password, token, lastLogin.
Dot notation for nested keys — use a dot to target fields inside nested objects. address.city keeps or removes only the city field inside the address object, leaving other nested fields intact. Arrays of objects are also supported — the filter is applied to every element in the array.
Examples
| Input JSON (abridged) | Keep: name, address.city | Remove: password, createdAt |
|---|---|---|
| {"name":"Ravi","email":"ravi@example.com","password":"secret","address":{"city":"Ahmedabad","pin":"380015"},"createdAt":"2024-01-10"} | {"name":"Ravi","address":{"city":"Ahmedabad"}} | {"name":"Ravi","email":"ravi@example.com","address":{"city":"Ahmedabad","pin":"380015"}} |
In Keep mode with keys name, address.city: only name and the nested address.city survive. In Remove mode with keys password, createdAt: those two fields are deleted and everything else is preserved.
Where Key Filtering Helps
- ▸Redacting sensitive fields before logging — Strip password, token, ssn or creditCard before writing a request/response to application logs.
- ▸Trimming a payload for a frontend — Send only the fields a UI actually needs instead of the full backend model.
- ▸Sanitizing data for a support ticket — Remove customer PII before pasting a JSON payload into a bug report or Slack thread.
- ▸Building a smaller test fixture — Keep only the fields relevant to the test case, dropping noise that makes fixtures hard to read.
Filtering Keys in Code
JavaScript — keep mode:
function pick(obj, keys) {
const out = {};
for (const key of keys) {
const value = key.split(".").reduce((acc, k) => acc?.[k], obj);
if (value !== undefined) out[key.split(".").pop()] = value;
}
return out;
}Python — remove mode:
def omit(obj: dict, keys: list[str]) -> dict:
return {k: v for k, v in obj.items() if k not in keys}