Proxy Community
Best way to JSON Python get all values for key? Need help! or How to JSON Python get all values for k - Printable Version

+- Proxy Community (https://proxycommunity.com/forum)
+-- Forum: Technical Community Support (https://proxycommunity.com/forum/forum-technical-community-support)
+--- Forum: API and Development (https://proxycommunity.com/forum/forum-api-and-development)
+--- Thread: Best way to JSON Python get all values for key? Need help! or How to JSON Python get all values for k (/thread-best-way-to-json-python-get-all-values-for-key-need-help-or-how-to-json-python-get-all-values-for-k)

Pages: 1 2


Best way to JSON Python get all values for key? Need help! or How to JSON Python get all values for k - MaskedWalkerX - 26-08-2024

"Struggling to json python get all values for key – any tips?"

Hey everyone,

I’ve been working with some nested JSON data in Python and hitting a wall.

Need to json python get all values for a specific key, but some are buried deep. Tried looping and recursive approaches, but it feels clunky.

Is there a cleaner or built-in way to do this? Maybe a one-liner or a library I’m missing?

Example:
```python
data = {"a": 1, "b": {"a": 2}, "c": [{"a": 3}]}
# Need all values for key "a" → [1, 2, 3]
```

Any help or shortcuts would be awesome! Thanks in advance.

(Also, if you’ve got a fav tool for this, lmk!)


“” - proxyRun99 - 13-01-2025

Hey! For json python get all values for key, I'd recommend using `jsonpath-ng`. It's a lifesaver for nested JSON.

Example:
```python
from jsonpath_ng import parse
expr = parse('$..a')
matches = [match.value for match in expr.find(data)]
```
Super clean and handles deep nesting. Check out the docs [here](https://github.com/h2non/jsonpath-ng).


“” - MaskedCipherX - 17-01-2025

Ugh, nested JSON is the worst. I feel your pain!

For a quick fix, try this recursive function:
```python
def get_values(obj, key):
if isinstance(obj, dict):
for k, v in obj.items():
if k == key:
yield v
yield from get_values(v, key)
elif isinstance(obj, list):
for item in obj:
yield from get_values(item, key)
```
Call it with `list(get_values(data, 'a'))`. Works like a charm!


“” - vpnSwift88 - 26-01-2025

If you're lazy like me, just use `jq` from the command line:
```bash
echo 'your_json' | jq '.. | .a? // empty'
```
Not pure Python, but saves time. For Python, `jsonpath-ng` is solid too.


“” - AnonVortex99 - 01-03-2025

Why not use a simple list comprehension with recursion?

```python
def find_keys(d, target):
if isinstance(d, dict):
return [d[target]] + [v for k, v in d.items() if isinstance(v, (dict, list)) for v in find_keys(v, target)]
elif isinstance(d, list):
return [v for item in d for v in find_keys(item, target)]
return []
```
Not a one-liner, but gets the job done for json python get all values for key.


“” - maskedGlide99 - 03-03-2025

Honestly, I just use `pandas` for this.

```python
import pandas as pd
df = pd.json_normalize(data, sep='_')
print(df.filter(like='a').values.flatten())
```
Might be overkill, but it's handy if you're already using pandas.


“” - CipherDrifterX - 09-03-2025

Check out `glom`! It's a bit quirky but powerful for nested data.

```python
from glom import glom
result = glom(data, ('a',)) # but you'll need to tweak for deep nesting
```
Docs: [glom](https://glom.readthedocs.io/). Not perfect, but worth a look.


“” - MaskedWalkerX - 10-03-2025

Wow, thanks for all the suggestions! Didn’t realize there were so many ways to json python get all values for key.

Tried the recursive function and it worked perfectly. Also gave `jsonpath-ng` a shot—super clean!

Still struggling with super messy data though. Anyone got tips for handling inconsistent key names? Like sometimes it’s `'a'` and other times `'A'`?

Appreciate the help!


“” - AnonVortex99 - 18-03-2025

For a no-library solution, this works:

```python
def extract_values(obj, key):
arr = []
if isinstance(obj, dict):
for k, v in obj.items():
if k == key:
arr.append(v)
arr.extend(extract_values(v, key))
elif isinstance(obj, list):
for item in obj:
arr.extend(extract_values(item, key))
return arr
```
Call it with `extract_values(data, 'a')`. Simple and effective.


“” - proxyLeap99 - 20-03-2025

If you're into one-liners (kinda), try this:

```python
values = [y for x in __import__('json').loads(__import__('json').dumps(data)) for y in ([x['a']] if 'a' in x else [])]
```
But... maybe don't. It's ugly. Stick with recursion or a lib.