#mxd-access-json

1 messages · Page 1 of 1 (latest)

haughty umbraBOT
bright wasp
#

Can you share an example request where you're hitting this?

#

eg, req_123

limpid stratus
#

productsubscription = event['data']['object']['items']['data']['plan']['id'] TypeError: string indices must be integers

#

im trying to get the id value, any tips on how to achieve this goal?

bright wasp
#

Is plan an object in this context? It's possible you just need to remove that final ['id']

limpid stratus
#

ok thanks for the help!

woven osprey
#

mxd-access-json

#

@limpid stratus the second data is an array and you don't need the bracket if you have deserialized the Event already so you should be able to do event.data.object.items.data[0].plan.id

limpid stratus
#
AttributeError: 'dict' object has no attribute 'data'
woven osprey
#

ah you use python I assume? Sorry you gave no context at all here. But stripe-python has an edge-case because we use dict for all objects so you have to special-case items too
event.data.objectitems.data[0].plan.id

limpid stratus
#

yeah its so tricky, same error ```

productsubscription = event.data.objectitems.data[0].plan.id

AttributeError: 'dict' object has no attribute 'data'```

woven osprey
limpid stratus
#

yeah im using the exact same code

#

ill keep trying to parse the json until i get the id

woven osprey
#

well you're not since that code de-serializes an Event and shows you how to cast then in another resource

#

and you seem to just access the JSON which you don't need to

#

can you share real code with multiple lines around this?

#
        payment_intent = event['data']['object']  # contains a stripe.PaymentIntent```
#

like we say this, so event['data']['object'] in your case should be a stripe.Subscription right?

limpid stratus
#

yeah

woven osprey
#

You said "yeah" but is it? What did you try What's your exact code? What do you see when you log?

limpid stratus
#

testing it now

limpid stratus
#
[Tue Nov 29 17:48:21.522000 2022] [wsgi:error] [pid 483567:tid 140415815821056] [client 54.187.174.169:50379]   File "/usr/local/lib/python3.7/dist-packages/stripe/stripe_object.py", line 85, in __getattr__
[Tue Nov 29 17:48:21.522004 2022] [wsgi:error] [pid 483567:tid 140415815821056] [client 54.187.174.169:50379]     raise AttributeError(*err.args)
[Tue Nov 29 17:48:21.522008 2022] [wsgi:error] [pid 483567:tid 140415815821056] [client 54.187.174.169:50379] AttributeError: objectitems```
woven osprey
#

what is objectitems?

limpid stratus
#
    if event['type'] == 'customer.subscription.created':
      subscription = event['data']['object']
      #activemembership = request['data']['object']
      activemembership = 'YES'
      # bronze: price_1M2H21LOEVMWAWDDt5NZ1Qki // object>data>object>items>data>0>plan>id
      #productsubscription = event['data']['object']['items']['data']['plan']['id']
      productsubscription = event.data.objectitems.data[0].plan.id
woven osprey
#

that doesn't give me more info, that's just more code. What's objectitems? What does subscription look like? What don't you use that object?

limpid stratus
#

whats objectitems idk you wrote that part productsubscription = event.data.objectitems.data[0].plan.id

#

trying again with a dot after object

woven osprey
#

ahhhhh sorry, I mistyped, didn't realize that came from me

#
    if event['type'] == 'customer.subscription.created':
      subscription = event['data']['object']
      price_id = subscription['items'].data[0].price.id```
#

this should work

#

['items'] is special for stripe-python, it's not needed for anything else but that specific property name

limpid stratus
#

it worked!

#

im so thankful for your help I will let someone know

woven osprey
#

Of course, I'm sorry it's such a pain to grasp webhooks in python

#

we want to add clear "types" to stripe-python in the future for all resources which will hopefully makes this way easier

limpid stratus
#

thanks again