Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Get value in json

Tags:

python

json

I need get values from key "color" in json using python, only for first level

json dumps:

[{"color": "red", "value": "10"}, {"color": "blue", "value": [{"color": "black", "value": "15"}]}]

I need:

[ 'red', 'blue']

I tried:

my_values = my_json_a.get('color')

But with error:

Error Contents: 'list' object has no attribute 'get'
like image 934
Roney rogerio Avatar asked May 16 '26 20:05

Roney rogerio


1 Answers

Try:

data = [{"color": "red", "value": "10"}, {"color": "blue", "value": [{"color": "black", "value": "15"}]}]
results = [x.get('color', None) for x in data]
print results

Output:

['red', 'blue']
like image 55
Andrés Pérez-Albela H. Avatar answered May 19 '26 09:05

Andrés Pérez-Albela H.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!