0

I keep scratching my head why it is not printing json content I need. Anyone know what I'm doing wrong ?

This is the dictionary

> "listinginfo": {
>     "438309609514180554": {
>       "listingid": "438309609514180554",
>       "price": 35,
>       "fee": 4,
>       "publisher_fee_app": 730,
>       "publisher_fee_percent": "0.10000000149011612",
>       "currencyid": "2003",
>       "steam_fee": 1,
>       "publisher_fee": 3,
>       "converted_price": 50,
>       "converted_fee": 7,
>       "converted_currencyid": "2020",
>       "converted_steam_fee": 2,
>       "converted_publisher_fee": 5,
>       "converted_price_per_unit": 50,
>       "converted_fee_per_unit": 7,
>       "converted_steam_fee_per_unit": 2,
>       "converted_publisher_fee_per_unit": 5,
>       "asset": {
>         "currency": 0,
>         "appid": 730,
>         "contextid": "2",
>         "id": "1579403640",
>         "amount": "1",
>         "market_actions": [
>           {

The code + I need the values for the keys I want to print:

while 1:
    r = requests.get(url, headers=headers)
    listingInfoStr = r.content
    result= ujson.loads(listingInfoStr)
    listingInfoJson= result['listinginfo']
    for listingdata in listingInfoJson: 
        print listingdata.get('listingId')
        print listingdata.get('subTotal') 
        print listingdata.get('feeAmount')
        print listingdata.get('totalPrice')
    time.sleep(10)

Thank you for your time.

1
  • Cause listingInfoJson not iterable? it's just a dict Commented Feb 12, 2015 at 22:27

2 Answers 2

3

You may use requests.Response.json method to get parsed JSON:

r = requests.get(url, headers=headers)
listingInfoJson = r.json()['listinginfo']
Sign up to request clarification or add additional context in comments.

2 Comments

please note that listingInfoJson will actually be a Python dict, and not a JSON string.
I don't want to use any other json parser apart from ujson, sorry.
0

I ran your code and it looks like listingInfoJson comes back as a dict not a list. So when you iterate over it it's just pulling the keys.

You're calling the .get method on a unicode object, which will give you an AttributeError. You can run this code in to different ways:

for listingdata in listingInfoJson: 
    print listingInfoJson[listingdata].get('listingid')
    print listingInfoJson[listingdata].get('subTotal') 
    print listingInfoJson[listingdata].get('feeAmount')
    print listingInfoJson[listingdata].get('totalPrice')

or the better way of (edited for comments):

if listingInfoJson:
    for key, value in listingInfoJson.iteritems():
        print value.get('listingid')
        print value.get('subTotal') 
        print value.get('feeAmount')
        print value.get('totalPrice')
else:
    print "listingInfoJson is empty"

I'd also check your key values, listingId should be listingid

4 Comments

Thanks, worked ! i prefer the "data.iteritems()" way you just showed me, i'm going to research this more, thank you again.
Wanted to ask you, currently "for" statement prints the values, is it possible to add "else" statement+print"no key/value found" if there are not key, value ?
Updated for what I think you meant.
Awesome! tried it and it's perfect ! Gracias mr.TimberlakeCoding !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.