First time working with JSON in Rails and trying to figure out something which seems like it should be quite basic. I’m working w/ the Stripe Rails Library and trying to parse out and set one of the JSON elements to a variable.
Here is my actual attempt:
coupon = Stripe::Coupon.retrieve(self.discount_code)
parsed_json = ActiveSupport::JSON.decode(coupon)
discount_amount = parsed_json["percent_off"]
This line works:
coupon = Stripe::Coupon.retrieve("save50percent")
Running “puts coupon” in the console shows the JSON string
JSON: {“id”:”test50″,”duration”:”once”,”duration_in_months”:null,”livemode”:false,”max_redemptions”:null,”object”:”coupon”,”percent_off”:50,”redeem_by”:null,”times_redeemed”:0}
but trying to run:
parsed_json = ActiveSupport::JSON.decode(coupon)
results in the following error:
TypeError: can't convert Stripe::Coupon into String
I haven’t even gotten to trying to parse out the value from the JSON – which i’m sure i’ve approached incorrectly.
Thanks
Your error message says it all:
couponis aStripe::Coupon, not aString. Try using the availableto_jsonmethod