turtle02 turtle02 - 7 months ago 22
Python Question

Python Json Parseing for output

I am new to json parsing but I want to get salePrice,name,modelnumber from this json response. The response seems to come all on 1 line and I am not having any luck getting the data.

`{u'sort': u'relevance', u'items': [{u'marketplace': False, u'imageEntities': [{u'entityType': u'SECONDARY', u'thumbnailImage': u'http://i5.walmartimages.com/dfw/dce07b8c-b23c/k2-_69d110a4-cba1-4d8b-a1c5-3892ef5b4b43.v1.jpg', u'largeImage': u'http://i5.walmartimages.com/dfw/dce07b8c-3397/k2-_ae913276-674a-4ef3-9e74-b53a586c294d.v1.jpg', u'mediumImage': u'http://i5.walmartimages.com/dfw/dce07b8c-bc42/k2-_86541363-b442-468e-8601-88273996cd30.v1.jpg'}, {u'entityType': u'SECONDARY', u'thumbnailImage': u'http://i5.walmartimages.com/dfw/dce07b8c-30ec/k2-_e470778f-a046-452a-9436-8d77ad484bbe.v1.jpg', u'largeImage': u'http://i5.walmartimages.com/dfw/dce07b8c-3661/k2-_54ec03e5-52ed-40ff-ae67-e25cfceff840.v1.jpg', u'mediumImage': u'http://i5.walmartimages.com/dfw/dce07b8c-98ea/k2-_08fa39a0-73cf-444f-9c83-30ddff3b5d61.v1.jpg'}, {u'entityType': u'SECONDARY', u'thumbnailImage': u'http://i5.walmartimages.com/dfw/dce07b8c-ea17/k2-_ec99e602-ae9e-4fb7-ba9d-1f00d1c068fa.v1.jpg', u'largeImage': u'http://i5.walmartimages.com/dfw/dce07b8c-b56f/k2-_12d4e3c1-454f-4fb0-9036-e72f7efca220.v1.jpg', u'mediumImage': u'http://i5.walmartimages.com/dfw/dce07b8c-c5ea/k2-_4b471514-753d-47bf-a82e-f8398aa7dda5.v1.jpg'}, {u'entityType': u'SECONDARY', u'thumbnailImage': u'http://i5.walmartimages.com/dfw/dce07b8c-1ba6/k2-_55347198-badb-47fc-ba68-d77973bf9a6d.v1.jpg', u'largeImage': u'http://i5.walmartimages.com/dfw/dce07b8c-72fc/k2-_90b99fa7-4016-40e4-8998-7ea93ea30e98.v1.jpg', u'mediumImage': u'http://i5.walmartimages.com/dfw/dce07b8c-5d76/k2-_4051f745-f43f-48b3-86b6-f82d9e999fca.v1.jpg'Traceback (most recent call last):
}, {u'entityType': u'SECONDARY', u'thumbnailImage': u'http://i5.walmartimages.com/dfw/dce07b8c-b0d0/k2-_526f7d4b-212a-4f60-b090-116f481a132b.v1.jpg', u'largeImage': u'http://i5.walmartimages.com/dfw/dce07b8c-a419/k2-_d986683e-46f1-4c58-8594-cb1ccd3cfdb5.v1.jpg', u'mediumImage': u'http://i5.walmartimages.com/dfw/dce07b8c-cafc/k2-_91678e98-52e5-4461-8587-0007f84dee24.v1.jpg'}, {u'entityType': u'PRIMARY', u'thumbnailImage': u'http://i5.walmartimages.com/dfw/dce07b8c-4cc9/k2-_8a40e4a9-ae3c-4df7-a9fc-71b48f3a7970.v1.jpg', u'largeImage': u'http://i5.walmartimages.com/dfw/dce07b8c-f9fe/k2-_e5b07842-8a7f-4de3-a212-ce36b44416fa.v1.jpg', u'mediumImage': u'http://i5.walmartimages.com/dfw/dce07b8c-a2c0/k2-_2e9d5b96-eadb-45f0-8c56-9d04adc941fa.v1.jpg'}], u'numReviews': 4, u'productUrl': u'http://c.affil.walmart.com/t/api02?l=http%3A%2F%2Fwww.walmart.com%2Fip%2FThe-Evil-Within-Xbox-One%2F26096326%3Faffp1%3Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi', u'customerRatingImage': u'http://i2.walmartimages.com/i/CustRating/4_8.gif', u'addToCartUrl': u'http://c.affil.walmart.com/t/api02?l=http%3A%2F%2Faffil.walmart.com%2Fcart%2FaddToCart%3Fitems%3D26096326%7C1%26affp1%3Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi', u'shortDescription': u"Developed by Shinji Mikami - creator of the seminal Resident Evil series - and the talented team at Tango Gameworks, The Evil Within embodies the meaning of pure survival horror. Experience a disturbing reality as you try to break free from warped machinations that could only exist in the most horrifying worlds. Face twisted creatures and experience true terror, all heightened by cutting edge lighting and animation made possible by a customized version of the id Tech 5 engine. While investigating the scene of a gruesome mass murder, Detective Sebastian Castellanos and his partners encounter a mysterious and powerful force. After seeing the slaughter of fellow officers, Sebastian is ambushed and knocked unconscious. When he awakens, he finds himself in a deranged world where hideous creatures wander among the dead. Facing unimaginable terror, and fighting for survival, Sebastian embarks on a frightening journey to unravel what's behind this evil force.", u'customerRating': u'4.75', u'affiliateAddToCartUrl': u'http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&RD_PARM1=http%253A%252F%252Faffil.walmart.com%252Fcart%252FaddToCart%253Fitems%253D26096326%257C1%2526affp1%253Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%2526affilsrc%253Dapi', u'mediumImage': u'http://i5.walmartimages.com/dfw/dce07b8c-a2c0/k2-_2e9d5b96-eadb-45f0-8c56-9d04adc941fa.v1.jpg', u'productTrackingUrl': u'http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&RD_PARM1=http%253A%252F%252Fwww.walmart.com%252Fip%252FThe-Evil-Within-Xbox-One%252F26096326%253Faffp1%253Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%2526affilsrc%253Dapi', u'stock': u'Available', u'categoryNode': u'2636_1040579', u'giftOptions': {u'allowGiftWrap': False, u'allowGiftMessage': False, u'allowGiftReceipt': False}, u'largeImage': u'http://i5.walmartimages.com/dfw/dce07b8c-f9fe/k2-_e5b07842-8a7f-4de3-a212-ce36b44416fa.v1.jpg', u'parentItemId': 26096326, u'thumbnailImage': u'http://i5.walmartimages.com/dfw/dce07b8c-4cc9/k2-_8a40e4a9-ae3c-4df7-a9fc-71b48f3a7970.v1.jpg', u'bundle': False, u'longDescription': u"<ul><li>Pure Survival Horror Returns - Shinji Mikami, the father of survival horror, is back to direct a chilling new game wrapped in haunting narrative. Tension and anxiety heighten dramatically as you explore the game's tortured world. You'll have limited resources at your disposal as you fight for your survival and experience profound fear in this perfect blend of horror and action.</li><li>Brutal Traps and Twisted Creatures - Face unthinkable horrors and cruel traps as you struggle to survive against overwhelming odds. Turn evil against itself by using the same diabolical devices against overwhelming deadly creatures.</li><li>Unknown Threats in an Uncertain World - Mysterious and wicked fears loom ahead in a world that warps and twists around you. Corridors, walls, doors, and entire buildings change in real-time, ensnaring players in a reality where threats can appear at any time and from any direction.</li><li>The New Face of Horror - Highly-crafted environments, horrifying anxiety, and an intricate story weave together to create an immersive world that will bring players to the height of tension. Defeat insurmountable terror and experience the ultimate thrill by discovering The Evil Within.</li><li>ESRB Rating: MATURE with Blood and Gore, Strong Language, and Intense Violence</li></ul>", u'standardShipRate': 4.97, u'categoryPath': u'Video Games/Video Games', u'itemId': 26096326, u'name': u'The Evil Within (Xbox One)', u'upc': u'093155118539', u'modelNumber': u'093155118539', u'salePrice': 49.96, u'availableOnline': True, u'msrp': 59.99}], u'facets': [], u'start': 1, u'numItems': 1, u'query': u'093155118539', u'totalResults': 1, u'responseGroup': u'base'}

Here is my code.

import json
import requests


def getPrice(request):
try:
price = request.find('"49.96"')
print price
return price
except:
print'fail Price'

def getData():
request = requests.get('http://api.walmartlabs.com/v1/search?query=093155118539&format=json&apiKey=KEYSHOULDGOHERE')
data = request.json()
print data #where I get my output
price = getPrice(request)

getData()


Link to api doc - I do not have a throw away API key to provide.
https://developer.walmartlabs.com/io-docs

Answer

You need to know the format of the response to parse it. I saw, that you used requests.get().

After your get request

request = requests.get(...)

request.json() will return a dict object of your JSON response. It looks like that (after formatted, used the codes like of @Alex):

{u'sort': u'relevance', u'items': [{u'marketplace': False,
                                        u'imageEntities': [
                                            {u'entityType': u'SECONDARY',
                                             u'thumbnailImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-b23c/k2-_69d110a4-cba1-4d8b-a1c5-3892ef5b4b43.v1.jpg',
                                             u'largeImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-3397/k2-_ae913276-674a-4ef3-9e74-b53a586c294d.v1.jpg',
                                             u'mediumImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-bc42/k2-_86541363-b442-468e-8601-88273996cd30.v1.jpg'},
                                            {u'entityType': u'SECONDARY',
                                             u'thumbnailImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-30ec/k2-_e470778f-a046-452a-9436-8d77ad484bbe.v1.jpg',
                                             u'largeImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-3661/k2-_54ec03e5-52ed-40ff-ae67-e25cfceff840.v1.jpg',
                                             u'mediumImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-98ea/k2-_08fa39a0-73cf-444f-9c83-30ddff3b5d61.v1.jpg'},
                                            {u'entityType': u'SECONDARY',
                                             u'thumbnailImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-ea17/k2-_ec99e602-ae9e-4fb7-ba9d-1f00d1c068fa.v1.jpg',
                                             u'largeImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-b56f/k2-_12d4e3c1-454f-4fb0-9036-e72f7efca220.v1.jpg',
                                             u'mediumImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-c5ea/k2-_4b471514-753d-47bf-a82e-f8398aa7dda5.v1.jpg'},
                                            {u'entityType': u'SECONDARY',
                                             u'thumbnailImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-1ba6/k2-_55347198-badb-47fc-ba68-d77973bf9a6d.v1.jpg',
                                             u'largeImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-72fc/k2-_90b99fa7-4016-40e4-8998-7ea93ea30e98.v1.jpg',
                                             u'mediumImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-5d76/k2-_4051f745-f43f-48b3-86b6-f82d9e999fca.v1.jpg'
                                             }, {
                                                u'entityType': u'SECONDARY',
                                                u'thumbnailImage':
                                                    u'http://i5.walmartimages.com/dfw/dce07b8c-b0d0/k2-_526f7d4b-212a-4f60-b090-116f481a132b.v1.jpg',
                                                u'largeImage':
                                                    u'http://i5.walmartimages.com/dfw/dce07b8c-a419/k2-_d986683e-46f1-4c58-8594-cb1ccd3cfdb5.v1.jpg',
                                                u'mediumImage':
                                                    u'http://i5.walmartimages.com/dfw/dce07b8c-cafc/k2-_91678e98-52e5-4461-8587-0007f84dee24.v1.jpg'},
                                            {You have did that
                                                u'entityType': u'PRIMARY',
                                                u'thumbnailImage':
                                                    u'http://i5.walmartimages.com/dfw/dce07b8c-4cc9/k2-_8a40e4a9-ae3c-4df7-a9fc-71b48f3a7970.v1.jpg',
                                                u'largeImage':
                                                    u'http://i5.walmartimages.com/dfw/dce07b8c-f9fe/k2-_e5b07842-8a7f-4de3-a212-ce36b44416fa.v1.jpg',
                                                u'mediumImage':
                                                    u'http://i5.walmartimages.com/dfw/dce07b8c-a2c0/k2-_2e9d5b96-eadb-45f0-8c56-9d04adc941fa.v1.jpg'}],
                                        u'numReviews': 4,
                                        u'productUrl':
                                            u'http://c.affil.walmart.com/t/api02?l=http%3A%2F%2Fwww.walmart.com%2Fip%2FThe-Evil-Within-Xbox-One%2F26096326%3Faffp1%3Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi',
                                        u'customerRatingImage':
                                            u'http://i2.walmartimages.com/i/CustRating/4_8.gif',
                                        u'addToCartUrl':
                                            u'http://c.affil.walmart.com/t/api02?l=http%3A%2F%2Faffil.walmart.com%2Fcart%2FaddToCart%3Fitems%3D26096326%7C1%26affp1%3Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi',
                                        u'shortDescription': u"Developed by "
                                                             u"Shinji Mikami "
                                                             u"- creator of "
                                                             u"the seminal "
                                                             u"Resident Evil "
                                                             u"series - and "
                                                             u"the talented "
                                                             u"team at Tango "
                                                             u"Gameworks, "
                                                             u"The Evil "
                                                             u"Within "
                                                             u"embodies the "
                                                             u"meaning of "
                                                             u"pure survival "
                                                             u"horror. "
                                                             u"Experience a "
                                                             u"disturbing "
                                                             u"reality as "
                                                             u"you try to "
                                                             u"break free "
                                                             u"from warped "
                                                             u"machinations "
                                                             u"that could "
                                                             u"only exist in "
                                                             u"the most "
                                                             u"horrifying "
                                                             u"worlds. Face "
                                                             u"twisted "
                                                             u"creatures and "
                                                             u"experience "
                                                             u"true terror, "
                                                             u"all "
                                                             u"heightened by "
                                                             u"cutting edge "
                                                             u"lighting and "
                                                             u"animation "
                                                             u"made possible "
                                                             u"by a "
                                                             u"customized "
                                                             u"version of "
                                                             u"the id Tech 5 "
                                                             u"engine. While "
                                                             u"investigating "
                                                             u"the scene of "
                                                             u"a gruesome "
                                                             u"mass murder, "
                                                             u"Detective "
                                                             u"Sebastian "
                                                             u"Castellanos "
                                                             u"and his "
                                                             u"partners "
                                                             u"encounter a "
                                                             u"mysterious "
                                                             u"and powerful "
                                                             u"force. After "
                                                             u"seeing the "
                                                             u"slaughter of "
                                                             u"fellow "
                                                             u"officers, "
                                                             u"Sebastian is "
                                                             u"ambushed and "
                                                             u"knocked "
                                                             u"unconscious. "
                                                             u"When he "
                                                             u"awakens, "
                                                             u"he finds "
                                                             u"himself in a "
                                                             u"deranged "
                                                             u"world where "
                                                             u"hideous "
                                                             u"creatures "
                                                        salePrice,name,modelnumber     u"wander among "
                                                             u"the dead. "
                                                             u"Facing "
                                                             u"unimaginable "
                                                             u"terror, "
                                                             u"and fighting "
                                                             u"for survival, "
                                                             u"Sebastian "
                                                             u"embarks on a "
                                                             u"frightening "
                                                             u"journey to "
                                                             u"unravel "
                                                             u"what's behind "
                                                             u"this evil "
                                                             u"force.",
                                        u'customerRating': u'4.75',
                                        u'affiliateAddToCartUrl':
                                            u'http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&RD_PARM1=http%253A%252F%252Faffil.walmart.com%252Fcart%252FaddToCart%253Fitems%253D26096326%257C1%2526affp1%253Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%2526affilsrc%253Dapi',
                                        u'mediumImage':
                                            salePrice,name,modelnumbersalePrice,name,modelnumberu'http://i5.walmartimages.com/dfw/dce07b8c-a2c0/k2-_2e9d5b96-eadb-45f0-8c56-9d04adc941fa.v1.jpg',
                                        u'productTrackingUrl':
                                            u'http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&RD_PARM1=http%253A%252F%252Fwww.walmart.com%252Fip%252FThe-Evil-Within-Xbox-One%252F26096326%253Faffp1%253Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%2526affilsrc%253Dapi',
                                        u'stock': u'Available',
                                        u'categoryNode': u'2636_1040579',
                                        u'giftOptions': {
                                            u'allowGiftWrap': False,
                                            u'allowGiftMessage': False,
                                            u'allowGiftReceipt': False},
                                        u'largeImage':
                                            u'http://i5.walmartimages.com/dfw/dce07b8c-f9fe/k2-_e5b07842-8a7f-4de3-a212-ce36b44416fa.v1.jpg',
                                        u'parentItemId': 26096326,
                                        u'thumbnailImage':
                                            u'http://i5.walmartimages.com/dfw/dce07b8c-4cc9/k2-_8a40e4a9-ae3c-4df7-a9fc-71b48f3a7970.v1.jpg',
                                        u'bundle': False,
                                        u'longDescription':
                                            u"&lt;ul&gt;&lt;li&gt;Pure "
                                            u"Survival Horror Returns - "
                                            u"Shinji Mikami, the father of "
                                            u"survival horror, is back to "
                                            u"direct a chilling new game "
                                            u"wrapped in haunting narrative. "
                                            u"Tension and anxiety heighten "
                                            u"dramatically as you explore "
                                            u"the game's tortured world. "
                                            u"You'll have limited resources "
                                            u"at your disposal as you fight "
                                            u"for your survival and "
                                            u"experience profound fear in "
                                            u"this perfect blend of horror "
                                            u"and "
                                            u"action.&lt;/li&gt;&lt;li&gt;Brutal Traps and Twisted Creatures - Face unthinkable horrors and cruel traps as you struggle to survive against overwhelming odds. Turn evil against itself by using the same diabolical devices against overwhelming deadly creatures.&lt;/li&gt;&lt;li&gt;Unknown Threats in an Uncertain World - Mysterious and wicked fears loom ahead in a world that warps and twists around you. Corridors, walls, doors, and entire buildings change in real-time, ensnaring players in a reality where threats can appear at any time and from any direction.&lt;/li&gt;&lt;li&gt;The New Face of Horror - Highly-crafted environments, horrifying anxiety, and an intricate story weave together to create an immersive world that will bring players to the height of tension. Defeat insurmountable terror and experience the ultimate thrill by discovering The Evil Within.&lt;/li&gt;&lt;li&gt;ESRB Rating: MATURE with Blood and Gore, Strong Language, and Intense Violence&lt;/li&gt;&lt;/ul&gt;",
                                        u'standardShipRate': 4.97,
                                        u'categoryPath': u'Video Games/Video '
                                                         u'Games',
                                        u'itemId': 26096326,
                                        u'name': u'The Evil Within (Xbox One)',
                                        u'upc': u'093155118539',
                                        u'modelNumber': u'093155118539',
                                        u'salePrice': 49.96,
                                        u'availableOnline': True,
                                        u'msrp': 59.99}], u'facets': [],
     u'start': 1, u'numItems': 1, u'query': u'093155118539',
     u'totalResults': 1, u'responseGroup': u'base'}

You see in u'items' is a list object of items. Each element of items is a dict object of a item. --> In each element of u'items' you will find salePrice,name,modelNumber.

Attention: It's modelNumber, not modelnumber

fields_2_get = [u'salePrice',u'name',u'modelNumber']
data = response.json()
for item in data[u'items']:
   for field_name in fields_2_get:
      print(field + ": " + str(item.get(field_name, None))) 

Your request object doesn't have the function find(). So request.find("xxx") won't work.

Comments