Skip to content
Advertisement

How to get values from all instances of key in JSON in Python

I have a response from a post request:

JSON RESPONSE :

[ { “a”: 3, “b”: 2, “c”: 1, }, { “a”: 3, “b”: 2, “c”: 1 } ]

How would I get the value from all instances of “c” and add it to an array? What I have now is:

JavaScript

This doesn’t give me the answer I need though. Ideally the array would contain values [1,1].

If I loop through the response and try to append the value, I get:

TypeError: the JSON object must be str, bytes or bytearray, not list

Advertisement

Answer

You are getting that error because you cannot treat JSON as a dictionary yet, you must load it first

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement