I am currently trying to parse some data from a post request response and I keep getting this error: "TypeError: list indices must be integers or slices, not str"
Python Code
import requests
import json
count = 0
params = {'var1':'40', 'value':'143', 'itm':'1', 'param':'1'}
req = 'https://www.api.com/api/search'
data = requests.post(req, data = params).json()
print (data['result']['results']['name'])
JSON Response
{
"result":{
"count":1,
"totalCount":1,
"offset":0,
"queryTime":232,
"results":[
{
"rating":"4.0",
"productId":{
"upc":"143",
"ItemId":"143",
"productId":"143-prd"
},
"name":"Product",
"catagory":{
"name":"",
"CataId":1
},
"images":{
"thumbnailUrl":"http://api.com/img/static/product-image-50-50.png",
"largeUrl":"http://api.com/img/static/product-image-500-500.png"
},
"price":{
"price":13,
"isRealTime":true,
"currencyUnit":"USD"
},
"location":{
"unit":[],
"detailed":[]
},
"inventory":{
"quantity":1,
"status":"In Stock",
"isRealTime":true
},
"ratings":{
"rating":"3.1875",
"ratingUrl":"http://api.com/3_1875.gif"
},
"reviews":{
"reviewCount":"2"
},
"isItem":true,
"lUrl":"/l/Product-Name"
}
],
"performance":{
"enrichment":{
}
},
"query":{
"originalQuery":"143",
"actualQuery":"143",
"suggestedQueries":[
]
},
"algo":"jarvis",
"blacklist":false,
"cluster":{
"apiserver":{
"hostname":"site.api.com",
"pluginVersion":"1.0"
},
"searchengine":{
"hostname":"srch.site.api.com"
}
}
}
}
I did a similar piece of code but it was a get request and everything turned out fine.
data['result']['results']
is an array so you can't do ['name']
you need an int, you could add [0]
after['results']
and it should work. Then you can reference keys within the object in results
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With