Extracting all keys from a JSON object is one of the most common tasks when exploring unfamiliar API responses, building data pipelines, or documenting schemas. This guide covers four methods - from quick one-liners to full recursive implementations - so you can choose the right tool for the job.
Why Extract JSON Keys?
Before writing code to process a JSON response, you need to know what fields are available. Key extraction is especially useful when:
- Integrating a new third-party API for the first time
- Building field mappings for data transformations or ETL pipelines
- Documenting a JSON schema for your team
- Debugging why a parser cannot find a field you expect to be there
Method 1: JavaScript - Object.keys()
Object.keys() returns only the top-level keys of an object:
const json = {
"name": "Alice",
"age": 30,
"address": { "city": "London", "zip": "EC1A 1BB" }
};
console.log(Object.keys(json));
// ["name", "age", "address"]
This misses nested keys like address.city. For flat JSON this is enough, but for anything nested you need a recursive approach.
Method 2: Recursive JavaScript
To get every key path - including nested objects and arrays - write a recursive function:
function getAllKeys(obj, prefix = '') {
let keys = [];
for (const key in obj) {
if (!obj.hasOwnProperty(key)) continue;
const fullKey = prefix ? `${prefix}.${key}` : key;
keys.push(fullKey);
const value = obj[key];
if (Array.isArray(value)) {
value.forEach((item, i) => {
if (typeof item === 'object' && item !== null) {
keys = keys.concat(getAllKeys(item, `${fullKey}[${i}]`));
}
});
} else if (typeof value === 'object' && value !== null) {
keys = keys.concat(getAllKeys(value, fullKey));
}
}
return keys;
}
const json = JSON.parse(jsonString);
console.log(getAllKeys(json));
Method 3: Python
Python's approach is equally straightforward using a recursive generator:
import json
def get_all_keys(obj, prefix=''):
keys = []
if isinstance(obj, dict):
for k, v in obj.items():
full_key = f"{prefix}.{k}" if prefix else k
keys.append(full_key)
keys.extend(get_all_keys(v, full_key))
elif isinstance(obj, list):
for i, v in enumerate(obj):
keys.extend(get_all_keys(v, f"{prefix}[{i}]"))
return keys
with open('data.json') as f:
data = json.load(f)
print('\n'.join(get_all_keys(data)))
Method 4: JSON Keyper - No Code Required
If you just need to see the keys quickly without writing or running any code, JSON Keyper handles it instantly in your browser:
- Paste your JSON into the left input box
- Click Extract Keys
- View every key path in the right output box
- Click Copy to send them all to your clipboard
JSON Keyper handles nested objects and arrays automatically using the same dot notation and bracket notation as the code examples above - but with zero setup.
Which Method Should You Use?
| Scenario | Best Approach |
|---|---|
| Quick one-off exploration | JSON Keyper |
| Automated pipeline or script | Python recursive function |
| Browser-side JavaScript processing | JS recursive function |
| Top-level keys only, flat JSON | Object.keys() |
Extract JSON Keys Instantly
Paste any JSON into JSON Keyper and get every key path in one click - no code, no setup, completely free.
Open JSON Keyper