JSON result parsing

Ben Bacarisse ben.usenet at bsb.me.uk
Sat Jul 30 12:20:05 EDT 2016


TUA <kai.peters at gmail.com> writes:

> Calls to my REST api may either return a dict (for single results) or
> a list of dicts (for multiple results).

I think John's answer missed this part.

> I receive these results using the requests library.
>
> I want to retrieve the value for a key 'ID' but only if I have a
> single result and, obviously, if ID is present.

If r is the result of the request, just testing

  if 'ID' in r: ... whatever ...

would do.  The result will be False if r is a list or the single result
does not have an ID.

> How can I do this with pythonic elegance?

Ah, that I leave to others.

-- 
Ben.



More information about the Python-list mailing list