I'm trying to take some Stripe data provided to me in Zapier and convert two of the epoch times to human readable dates so the subscription period can be easily read. The below exact code was previously working flawlessly until a few days ago, now I receive the following error:
Traceback (most recent call last):
File "/usr/lib64/python2.7/json/__init__.py", line 251, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib64/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/var/runtime/awslambda/bootstrap.py", line 110, in decimal_serializer
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: time.struct_time(tm_year=2018, tm_mon=10, tm_mday=22, tm_hour=14, tm_min=9, tm_sec=58, tm_wday=0, tm_yday=295, tm_isdst=0) is not JSON serializable
Here is the previously working code:
import ast
import json
import time
data = "amount: 1000
currency: usd
description: None
discountable: True
id: sub_DfoojqxyubarmQ
livemode: True
metadata: {}
object: line_item
period: {u'start': 1540217398, u'end': 1542895798}
plan: {u'active': True, u'product': u'prod_fooj56ZAbarmaU',
u'transform_usage': None, u'name': u'Subscription - Monthly',
u'aggregate_usage': None, u'created': 1532982748, u'tiers_mode': None,
u'interval': u'month', u'tiers': None, u'object': u'plan', u'id':
u'plan_DKMmOfooQEbars', u'currency': u'usd', u'amount': 1000,
u'interval_count': 1, u'trial_period_days': None, u'livemode': True,
u'usage_type': u'licensed', u'metadata': {u'qb_sub_product': u'8'},
u'nickname': u'Subscription - Monthly', u'statement_descriptor':
u’SUBSCRIPTION’, u'billing_scheme': u'per_unit'}
proration: False
quantity: 1
subscription: None
subscription_item: si_DpfooWbarKUVIh
type: subscription"
#retrieve period data
period_begin = data.find('period:') + 8
period_end = data.find('plan:')
#convert period to JSON
period = ast.literal_eval(data[period_begin:period_end])
json_period = json.dumps(period)
json_period = json.loads(json_period)
#retrieve plan data
plan_begin = data.find('plan:') + 6
plan_end = data.rfind('}') + 1
#convert plan to JSON
plan = ast.literal_eval(data[plan_begin:plan_end])
json_plan = json.dumps(plan)
json_plan = json.loads(json_plan)
#period_start and period_end to human readable format
period_start = time.strftime("%-m/%-d/%Y"), time.localtime(json_period['start'])
period_end = time.strftime("%-m/%-d/%Y", time.localtime(json_period['end']))
output = [{'json_plan': json_plan, 'json_period': json_period, 'period_start': period_start, 'period_end': period_end}]
You have a typo. The parentheses ended time.strftime("%-m/%-d/%Y")
prematurely. What your code did was produce a 2 value tuple: format now()
to the default strftime format, and a time.localtime
from the epoch.
#period_start and period_end to human readable format
period_start = time.strftime("%-m/%-d/%Y", time.localtime(json_period['start']))
period_end = time.strftime("%-m/%-d/%Y", time.localtime(json_period['end']))