API Help

Matt Wheeler m at funkyhat.org
Wed Jun 14 17:16:57 EDT 2017


On Wed, 14 Jun 2017 at 21:47 Erik <python at lucidity.plus.com> wrote:

> On 14/06/17 21:38, Chris Angelico wrote:
> > On Thu, Jun 15, 2017 at 6:33 AM, Bradley Cooper
> > <brad at midwestaftermarket.com> wrote:
> >> I am working with an API and I get a return response in this format.
> >>
> >>
> >>
> [{"itemNumber":"75-5044","inventory":[{"warehouseCode":"UT-1-US","quantityAvailable":0.0000000000000},{"warehouseCode":"KY-1-US","quantityAvailable":0.0000000000000},{"warehouseCode":"TX-1-US","quantityAvailable":14.0000000000000},{"warehouseCode":"CA-1-US","quantityAvailable":4.0000000000000},{"warehouseCode":"AB-1-CA","quantityAvailable":1.0000000000000},{"warehouseCode":"WA-1-US","quantityAvailable":0.0000000000000},{"warehouseCode":"PO-1-CA","quantityAvailable":0.0000000000000}]}]
> >>
> >> What is the best way to read through the data?
> >
> > That looks like JSON.
>

Yes


> If the keys weren't quoted, I'd agree with you.
>

? JSON keys are quoted


> It looks like a REPL representation of a Python structure to me (list of
> dicts where the values could also be lists of dicts ...
>
> What makes it look like JSON to you? (I'm not arguing, I'm asking what
> I've missed).
>

On Wed, 14 Jun 2017 at 21:54 Bradley Cooper <brad at midwestaftermarket.com>
wrote:

> Yes it is not json, I did try that with no luck.


What did you try?

% python
Python 2.7.13 (default, Feb 17 2017, 23:41:27)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> s = raw_input()
[{"itemNumber":"75-5044","inventory":[{"warehouseCode":"UT1-US","quantityAvailable":0.0000000000000},{"warehouseCode":"KY1US","quantityAvailable":0.0000000000000},{"warehouseCode":"TX-1-US","quantityAvailable":14.0000000000000},{"warehouseCode":"CA-1-US","quantityAvailable":4.0000000000000},{"warehouseCode":"AB-1-CA","quantityAvailable":1.0000000000000},{"warehouseCode":"WA-1-US","quantityAvailable":0.0000000000000},{"warehouseCode":"PO-1-CA","quantityAvailable":0.0000000000000}]}]
>>> json.loads(s)
[{u'inventory': [{u'quantityAvailable': 0.0, u'warehouseCode': u'UT1-US'},
                 {u'quantityAvailable': 0.0, u'warehouseCode': u'KY1US'},
                 {u'quantityAvailable': 14.0, u'warehouseCode': u'TX-1-US'},
                 {u'quantityAvailable': 4.0, u'warehouseCode': u'CA-1-US'},
                 {u'quantityAvailable': 1.0, u'warehouseCode': u'AB-1-CA'},
                 {u'quantityAvailable': 0.0, u'warehouseCode': u'WA-1-US'},
                 {u'quantityAvailable': 0.0, u'warehouseCode': u'PO-1-CA'}],
  u'itemNumber': u'75-5044'}]

Looks like json to me, and to json.loads, which is probably more
authoritative :)
-- 

--
Matt Wheeler
http://funkyh.at



More information about the Python-list mailing list