Skip to content Skip to sidebar Skip to footer

Get Specific Json Elements With Requests

I want to make a JSON request with the Python library requests where I only obtain certain JSON objects. I know that it is really easy to process the JSON object obtained to only f

Solution 1:

Short Answer

To answer your question no, you can't but to understand why you need to know what is happening behind the scenes.

Behind the scenes

When you make a request such as r = requests.get('www.foo.bar') you are making a request to the server and you are viewing the result of that request when you do r.json(). This means that you cannot just get r[3]['data'] as you are parsing what the server sends to you unless the server only sends r[3]['data']. It may be possible to filter out everything else apart from that in the response processing but I am unaware of how to do it.

Solution 2:

You can't, if the server does not allow it. If the target server allows you to specify fields you want then you can send that field list in your request and server will return you only those fields in JSON. Otherwise your will have to parse full JSON response and get your desired fields.

Post a Comment for "Get Specific Json Elements With Requests"