[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-list
Subject:    Re: JSON result parsing
From:       Ben Bacarisse <ben.usenet () bsb ! me ! uk>
Date:       2016-07-30 16:20:05
Message-ID: 87wpk3w11m.fsf () bsb ! me ! uk
[Download RAW message or body]

TUA <kai.peters@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.
-- 
https://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic