0

I am currently parsing eBay data using eBay public API, I have figured out the way to parse the JSON structure except for some JSON elements.

Here is the JSON structure that I am looking at:

  {u'itemId': [u'370640300983'], u'isMultiVariationListing': [u'false'], u'globalId': [u'EBAY-US'], u'title': [u'DELL Latitude D630 Core 2 Duo 2GHz 1GB 80GB CD-RW/DVD WiFi Notebook 14" Laptop'], u'country': [u'US'], u'shippingInfo': [{u'expeditedShipping': [u'true'], u'shippingType': [u'Calculated'], u'handlingTime': [u'1'], u'shipToLocations': [u'US'], u'oneDayShippingAvailable': [u'false']}], u'galleryURL': [u'http://thumbs4.ebaystatic.com/pict/3706403009834040_1.jpg'], u'autoPay': [u'false'], u'location': [u'Saint Paul,MN,USA'], u'postalCode': [u'55114'], u'returnsAccepted': [u'true'], u'viewItemURL': [u'http://www.ebay.com/itm/DELL-Latitude-D630-Core-2-Duo-2GHz-1GB-80GB-CD-RW-DVD-WiFi-Notebook-14-Laptop-/370640300983?pt=Laptops_Nov05'], u'sellingStatus': [{u'currentPrice': [{u'@currencyId': u'USD', u'__value__': u'99.99'}], u'timeLeft': [u'P0DT0H13M10S'], u'convertedCurrentPrice': [{u'@currencyId': u'USD', u'__value__': u'99.99'}], u'bidCount': [u'4'], u'sellingState': [u'Active']}], u'paymentMethod': [u'PayPal', u'VisaMC', u'Discover'], u'primaryCategory': [{u'categoryId': [u'177'], u'categoryName': [u'PC Laptops & Netbooks']}], u'condition': [{u'conditionId': [u'3000'], u'conditionDisplayName': [u'Used']}], u'listingInfo': [{u'listingType': [u'Auction'], u'gift': [u'false'], u'bestOfferEnabled': [u'false'], u'startTime': [u'2012-08-15T23:28:05.000Z'], u'buyItNowAvailable': [u'false'], u'endTime': [u'2012-08-20T23:28:05.000Z']}]}

Data that I am currently parsing

370640300983
DELL Latitude D630 Core 2 Duo 2GHz 1GB 80GB CD-RW/DVD WiFi Notebook 14" Laptop
{u'@currencyId': u'USD', u'__value__': u'99.99'}

Second Element:

{u'itemId': [u'170892723100'], u'isMultiVariationListing': [u'false'], u'globalId': [u'EBAY-US'], u'title': [u'Dell Latitude D620 Laptop Core 2 Duo 2GHz  1GB Ram No HDD INCOMPLETE'], u'country': [u'US'], u'shippingInfo': [{u'expeditedShipping': [u'false'], u'handlingTime': [u'1'], u'shippingServiceCost': [{u'@currencyId': u'USD', u'__value__': u'24.0'}], u'oneDayShippingAvailable': [u'false'], u'shipToLocations': [u'US'], u'shippingType': [u'Flat']}], u'galleryURL': [u'http://thumbs1.ebaystatic.com/pict/1708927231004040_1.jpg'], u'autoPay': [u'false'], u'location': [u'Hughesville,PA,USA'], u'postalCode': [u'17737'], u'returnsAccepted': [u'true'], u'viewItemURL': [u'http://www.ebay.com/itm/Dell-Latitude-D620-Laptop-Core-2-Duo-2GHz-1GB-Ram-No-HDD-INCOMPLETE-/170892723100?pt=Laptops_Nov05'], u'sellingStatus': [{u'currentPrice': [{u'@currencyId': u'USD', u'__value__': u'20.01'}], u'timeLeft': [u'P0DT1H10M35S'], u'convertedCurrentPrice': [{u'@currencyId': u'USD', u'__value__': u'20.01'}], u'bidCount': [u'2'], u'sellingState': [u'Active']}], u'paymentMethod': [u'PayPal'], u'primaryCategory': [{u'categoryId': [u'177'], u'categoryName': [u'PC Laptops & Netbooks']}], u'condition': [{u'conditionId': [u'3000'], u'conditionDisplayName': [u'Used']}], u'listingInfo': [{u'listingType': [u'Auction'], u'gift': [u'false'], u'bestOfferEnabled': [u'false'], u'startTime': [u'2012-08-18T00:25:30.000Z'], u'buyItNowAvailable': [u'false'], u'endTime': [u'2012-08-21T00:25:30.000Z']}]}

Parsed elements of the second element:

170892723100
Dell Latitude D620 Laptop Core 2 Duo 2GHz  1GB Ram No HDD INCOMPLETE
{u'@currencyId': u'USD', u'__value__': u'20.01'}

If you see In both iteration of my code I am unable to get the u'value: element parse and get the actual price extract from the data structure:

Basically instead of {u'@currencyId': u'USD', u'__value__': u'20.01'} I would like to get 20.01 as the parsed value. Should I be using regular expression to parse it or is there a better way to do it?

Here is my code:
  data = json.load(urllib2.urlopen(url))
  #print data
  for item in data['findItemsByKeywordsResponse'][0]['searchResult'][0]['item']:
    print item
    for itemId in item['itemId']:
      print itemId
    for title in item['title']:
      print title
    for price in item['sellingStatus'][0]['currentPrice']:
      print price
    print '\n'
5
  • 1
    What about changing that line to print price['__value__']? Commented Aug 20, 2012 at 23:29
  • brilliant may be you should submit as an answer so I can accept it. Commented Aug 20, 2012 at 23:31
  • 1
    You went that far into the objects and lists with the [] syntax to ask if you need regex to do one more of these steps? Am I missing something? Commented Aug 20, 2012 at 23:31
  • I actually overlook @RocketDonkey infact opened my eyes..? Commented Aug 20, 2012 at 23:32
  • Sure, just posted it. Happy it helps! Commented Aug 20, 2012 at 23:34

2 Answers 2

2

Just do this:

for price in item['sellingStatus'][0]['currentPrice']:
      print float(price["__value__"])

Of course, it's a horrible idea to use floats for money, so you should either use the decimal module:

from decimal import Decimal

for price in item['sellingStatus'][0]['currentPrice']:
      print Decimal(price["__value__"])

Or parse it into an integer price in cents:

for price in item['sellingStatus'][0]['currentPrice']:
      dollars, cents = price["__value__"].split(".")
      print int(dollars) * 100 + int(cents)
Sign up to request clarification or add additional context in comments.

Comments

1

(Per comment above):

Try changing:

for price in item['sellingStatus'][0]['currentPrice']:
    print price

to

for price in item['sellingStatus'][0]['currentPrice']:
    print price['__value__']

Comments

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.