skip to Main Content

I have a function in python to extract data from an api response in json format. i want to extract ['attributes']['value'] of the ['Brand code'] from the response, but i get this error

ERROR:root:Update caused error list indices must be integers or slices, not str

Api response:

    {
"total": 143,
"count": 100,
"offset": 0,
"limit": 100,
"items": [
{
"id": 469539022,
"sku": "10311",
"thumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3098497459.jpg",
"unlimited": true,
"inStock": true,
"name": "FFFFF",
"nameTranslated": {
"ar": "FFFFF",
"en": ""
},
"categoryIds": [],
"categories": [],
"defaultCategoryId": 0,
"seoTitle": "",
"seoDescription": "",
"favorites": {
"count": 0,
"displayedCount": "0"
},
"attributes": [],
"files": [],
"relatedProducts": {
"productIds": [],
"relatedCategory": {
"enabled": false,
"categoryId": 0,
"productCount": 5
}
},
"combinations": [],
"dimensions": {
"length": 0,
"width": 0,
"height": 0
},
"shippingPreparationTime": {},
"showDeliveryTimeInStorefront": false,
"volume": 0,
"showOnFrontpage": 1,
"isSampleProduct": false,
"googleItemCondition": "NEW",
"isGiftCard": false,
"discountsAllowed": true,
"nameYourPriceEnabled": false,
"subscriptionSettings": {
"subscriptionAllowed": false,
"oneTimePurchaseAllowed": true,
"recurringChargeSettings": [
{
"recurringInterval": "MONTH",
"recurringIntervalCount": 1
}
]
},
"googleProductCategory": 166,
"googleProductCategoryName": "Apparel & Accessories",
"productCondition": "NEW",
"outOfStockVisibilityBehaviour": "HIDE"
},
{
"id": 468690345,
"sku": "10310",
"thumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763869.jpg",
"unlimited": true,
"inStock": true,
"name": "طقم ولادي",
"nameTranslated": {
"ar": "طقم ولادي",
"en": ""
},
"price": 170,
"priceInProductList": 170,
"defaultDisplayedPrice": 170,
"defaultDisplayedPriceFormatted": "$170",
"costPrice": 0,
"tax": {
"taxable": true,
"defaultLocationIncludedTaxRate": 0,
"enabledManualTaxes": [
85540417
],
"taxClassCode": "default"
},
"isShippingRequired": true,
"weight": 0,
"url": "https://www.albeyanfashion.com/store/#!/Tqm-wldy/p/468690345",
"created": "2022-05-26 13:31:42 +0000",
"updated": "2022-05-26 13:31:42 +0000",
"createTimestamp": 1653571902,
"updateTimestamp": 1653571902,
"productClassId": 0,
"enabled": true,
"options": [
{
"type": "RADIO",
"name": "عدد القطع بالسيري",
"nameTranslated": {
"ar": "عدد القطع بالسيري",
"en": ""
},
"choices": [
{
"text": "10",
"textTranslated": {
"ar": "10",
"en": ""
},
"priceModifier": 0,
"priceModifierType": "ABSOLUTE"
}
],
"defaultChoice": 0,
"required": false
},
{
"type": "RADIO",
"name": "سعر القطعة",
"nameTranslated": {
"ar": "سعر القطعة",
"en": ""
},
"choices": [
{
"text": "$17",
"textTranslated": {
"ar": "$17",
"en": ""
},
"priceModifier": 0,
"priceModifierType": "ABSOLUTE"
}
],
"defaultChoice": 0,
"required": false
},
{
"type": "RADIO",
"name": "العمر",
"nameTranslated": {
"ar": "العمر",
"en": ""
},
"choices": [
{
"text": "1 - 10",
"textTranslated": {
"ar": "1 - 10",
"en": ""
},
"priceModifier": 0,
"priceModifierType": "ABSOLUTE"
}
],
"defaultChoice": 0,
"required": false
}
],
"warningLimit": 0,
"fixedShippingRateOnly": false,
"fixedShippingRate": 0,
"shipping": {
"type": "GLOBAL_METHODS",
"methodMarkup": 0,
"flatRate": 0,
"disabledMethods": [],
"enabledMethods": []
},
"defaultCombinationId": 0,
"imageUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763867.jpg",
"smallThumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763866.jpg",
"hdThumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763868.jpg",
"originalImageUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763865.jpg",
"originalImage": {
"url": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763865.jpg",
"width": 960,
"height": 1280
},
"borderInfo": {
"dominatingColor": {
"red": 203,
"green": 202,
"blue": 195,
"alpha": 255
},
"homogeneity": false
},
"description": "<p><span style="font-size: 20px;">تشكيلة واسعة من افضل ماركات ملابس الاطفال التركية المصنعة من أجود أنواع الأقمشة ويمكنكم الشراء بسهولة و بأفضل أسعار الجملة.</span></p>",
"descriptionTranslated": {
"ar": "<p><span style="font-size: 20px;">تشكيلة واسعة من افضل ماركات ملابس الاطفال التركية المصنعة من أجود أنواع الأقمشة ويمكنكم الشراء بسهولة و بأفضل أسعار الجملة.</span></p>",
"en": ""
},
"galleryImages": [
{
"id": 1,
"url": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781020.jpg",
"thumbnail": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781021.jpg",
"originalImageUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781020.jpg",
"imageUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781022.jpg",
"hdThumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781023.jpg",
"thumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781024.jpg",
"smallThumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781021.jpg",
"width": 960,
"height": 1280,
"orderBy": 0,
"borderInfo": {
"dominatingColor": {
"red": 193,
"green": 193,
"blue": 188,
"alpha": 255
},
"homogeneity": false
}
},
{
"id": 2,
"url": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092755493.jpg",
"thumbnail": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092755494.jpg",
"originalImageUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092755493.jpg",
"imageUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092755495.jpg",
"hdThumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092755496.jpg",
"thumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092755497.jpg",
"smallThumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092755494.jpg",
"width": 960,
"height": 1280,
"orderBy": 1,
"borderInfo": {
"dominatingColor": {
"red": 212,
"green": 209,
"blue": 202,
"alpha": 255
},
"homogeneity": false
}
},
{
"id": 3,
"url": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763384.jpg",
"thumbnail": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763385.jpg",
"originalImageUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763384.jpg",
"imageUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763386.jpg",
"hdThumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763387.jpg",
"thumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763388.jpg",
"smallThumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763385.jpg",
"width": 960,
"height": 1280,
"orderBy": 2,
"borderInfo": {
"dominatingColor": {
"red": 213,
"green": 210,
"blue": 199,
"alpha": 255
},
"homogeneity": false
}
},
{
"id": 4,
"url": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763389.jpg",
"thumbnail": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763390.jpg",
"originalImageUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763389.jpg",
"imageUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763391.jpg",
"hdThumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763392.jpg",
"thumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763393.jpg",
"smallThumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763390.jpg",
"width": 960,
"height": 1280,
"orderBy": 3,
"borderInfo": {
"dominatingColor": {
"red": 198,
"green": 198,
"blue": 187,
"alpha": 255
},
"homogeneity": false
}
},
{
"id": 5,
"url": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781025.jpg",
"thumbnail": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781026.jpg",
"originalImageUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781025.jpg",
"imageUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781027.jpg",
"hdThumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781028.jpg",
"thumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781029.jpg",
"smallThumbnailUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781026.jpg",
"width": 960,
"height": 1280,
"orderBy": 4,
"borderInfo": {
"dominatingColor": {
"red": 189,
"green": 189,
"blue": 179,
"alpha": 255
},
"homogeneity": false
}
}
],
"media": {
"images": [
{
"id": "0",
"isMain": true,
"orderBy": 0,
"image160pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763866.jpg",
"image400pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763869.jpg",
"image800pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763868.jpg",
"image1500pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763867.jpg",
"imageOriginalUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763865.jpg"
},
{
"id": "1",
"isMain": false,
"orderBy": 1,
"image160pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781021.jpg",
"image400pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781024.jpg",
"image800pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781023.jpg",
"image1500pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781022.jpg",
"imageOriginalUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781020.jpg"
},
{
"id": "2",
"isMain": false,
"orderBy": 2,
"image160pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092755494.jpg",
"image400pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092755497.jpg",
"image800pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092755496.jpg",
"image1500pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092755495.jpg",
"imageOriginalUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092755493.jpg"
},
{
"id": "3",
"isMain": false,
"orderBy": 3,
"image160pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763385.jpg",
"image400pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763388.jpg",
"image800pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763387.jpg",
"image1500pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763386.jpg",
"imageOriginalUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763384.jpg"
},
{
"id": "4",
"isMain": false,
"orderBy": 4,
"image160pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763390.jpg",
"image400pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763393.jpg",
"image800pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763392.jpg",
"image1500pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763391.jpg",
"imageOriginalUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092763389.jpg"
},
{
"id": "5",
"isMain": false,
"orderBy": 5,
"image160pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781026.jpg",
"image400pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781029.jpg",
"image800pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781028.jpg",
"image1500pxUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781027.jpg",
"imageOriginalUrl": "https://d2j6dbq0eux0bg.cloudfront.net/images/63690252/3092781025.jpg"
}
]
},
"categoryIds": [
127443595,
127508562,
127918543
],
"categories": [
{
"id": 127918543,
"enabled": true
},
{
"id": 127508562,
"enabled": true
},
{
"id": 127443595,
"enabled": true
}
],
"defaultCategoryId": 127443595,
"seoTitle": "أطفال / ولادي",
"seoDescription": "ملابس أطفال ولادي - طقم ولادي",
"favorites": {
"count": 0,
"displayedCount": "0"
},
"attributes": [
{
"id": 119748758,
"name": "Brand code",
"nameTranslated": {
"ar": "Brand code",
"en": ""
},
"value": "239041117",
"valueTranslated": {
"ar": "239041117",
"en": ""
},
"show": "DESCR",
"type": "BRAND"
},
{
"id": 119748757,
"name": "ملاحظة",
"nameTranslated": {
"ar": "ملاحظة",
"en": ""
},
"value": "اختيار الألوان يتم عند البدء بتجهيز الطلبية",
"valueTranslated": {
"ar": "اختيار الألوان يتم عند البدء بتجهيز الطلبية",
"en": ""
},
"show": "DESCR",
"type": "UPC"
}
],
"files": [],
"relatedProducts": {
"productIds": [],
"relatedCategory": {
"enabled": false,
"categoryId": 0,
"productCount": 5
}
},
"combinations": [],
"dimensions": {
"length": 0,
"width": 0,
"height": 0
},
"shippingPreparationTime": {},
"showDeliveryTimeInStorefront": false,
"volume": 0,
"showOnFrontpage": 98,
"isSampleProduct": false,
"googleItemCondition": "NEW",
"isGiftCard": false,
"discountsAllowed": true,
"subtitle": "السعر المعروض للسيري كامل",
"subtitleTranslated": {
"ar": "السعر المعروض للسيري كامل",
"en": ""
},
"nameYourPriceEnabled": false,
"subscriptionSettings": {
"subscriptionAllowed": false,
"oneTimePurchaseAllowed": true,
"recurringChargeSettings": [
{
"recurringInterval": "MONTH",
"recurringIntervalCount": 1
}
]
},
"googleProductCategory": 166,
"googleProductCategoryName": "Apparel & Accessories",
"productCondition": "NEW",
"outOfStockVisibilityBehaviour": "HIDE" }

The function:

def listProducts(data):
    for item in data['items']:
        global cout
        name = item['name']
        seoName = item['seoTitle']
        price = item['price']
        itemUrl = item['url']
        ImageUrl = item['imageUrl']
        BrandCode = item['attributes']['value']

        custom_keyboard = [[InlineKeyboardButton("Open", url=itemUrl)]]
        reply_markup = InlineKeyboardMarkup(custom_keyboard)
        content = bot.send_photo(chat_id=chatId, photo=ImageUrl, caption=f"{name} - {seoName} - {BrandCode}  n <b>${price}</b>", parse_mode=telegram.ParseMode.HTML, reply_markup=reply_markup)

        time.sleep(1)
        cout += 1
        if cout == 20:
            time.sleep(60)
            cout = 0

    return content

2

Answers


  1. Chosen as BEST ANSWER

    I found the answer by trying. all i had to do is put a selector like this: BrandCode = item['attributes'][0]['value'] the zero is the selector.


  2. If you’re using an error handler with your Dispatcher/Application, make sure that it can actually show the full traceback. You can use e.g. an error handler based on this example. If you don’t use an error handle, the full traceback will be logged by python-telegram-bot by default.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search