API Help

justin walters walters.justin01 at gmail.com
Wed Jun 14 19:08:26 EDT 2017


On Wed, Jun 14, 2017 at 4:06 PM, justin walters <walters.justin01 at gmail.com>
wrote:

>
>
> On Wed, Jun 14, 2017 at 3:49 PM, Grant Edwards <grant.b.edwards at gmail.com>
> wrote:
>
>> On 2017-06-14, Erik <python at lucidity.plus.com> wrote:
>> > On 14/06/17 22:54, Ray Cote wrote:
>> >> Definitely JSON:
>> >>>>>
>> >> json.loads(“""[{"itemNumber":"75-5044","inventory":[{"wareho
>> useCode":"UT-1-US","quantityAvailable":0.0000000000000},{"wa
>> rehouseCode":"KY-1-US","quantityAvailable":0.0000000000000},
>> {"warehouseCode":"TX-1-US","quantityAvailable":14.0000000000
>> 000},{"warehouseCode":"CA-1-US","quantityAvailable":4.0000
>> 000000000},{"warehouseCode":"AB-1-CA","quantityAvailable":
>> 1.0000000000000},{"warehouseCode":"WA-1-US","quantityAvailab
>> le":0.0000000000000},{"warehouseCode":"PO-1-CA","quantityAva
>> ilable":0.0000000000000}]}]""")
>> [...]
>> >
>> > If that makes it definitely JSON, then this makes it definitely Python
>> ;) :
>> > >>>
>> > [{"itemNumber":"75-5044","inventory":[{"warehouseCode":"UT-
>> 1-US","quantityAvailable":0.0000000000000},{"warehouseCode
>> ":"KY-1-US","quantityAvailable":0.0000000000000},{"warehouse
>> Code":"TX-1-US","quantityAvailable":14.0000000000000},{"ware
>> houseCode":"CA-1-US","quantityAvailable":4.0000000000000},{"
>> warehouseCode":"AB-1-CA","quantityAvailable":1.0000000000000
>> },{"warehouseCode":"WA-1-US","quantityAvailable":0.000000000
>> 0000},{"warehouseCode":"PO-1-CA","quantityAvailable":0.0000000000000}]}]
>>
>> Indeed.
>>
>> > So, I think we're agreed that it's definitely one or the other.
>>
>> It's both a floor wax _and_ a dessert topping!
>>
>> --
>> Grant
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
> JSON and Python dictionaries have nearly the exact same syntax. That's why
> working with
> JSON in Python is such a joy! :)
>
> You can paste any valid JSON into a Python REPL and it will be interpreted
> as a Python
> dictionary.
>
> I can assure you with 99.9999~% confidence that the data you are receiving
> from the API
> is JSON data. You will find very few web APIs around nowadays that aren't
> using JSON or
> JSONB(binary representation of JSON).
>
> All that said, you can use the built in ``json`` module to convert the raw
> data into a Python
> dictionary in the following manner(assuming you are using Python 3.2+).
>
> ```
> import json
>
> response_data = "..." # This is the received JSON data as a binary string.
>
> data = json.loads(
>     str(response_data, encoding="utf8")
> )
>
> data["itemNumber"]
>
> >>> "75-5044"
> ```
>
> Hope that helps.
>

I should also specify that the data you have received is malformed.

JSON should always have an object as the top level structure, not an
array as in your data.



More information about the Python-list mailing list