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: coupon
is a Stripe::Coupon
, not a String
. Try using the available to_json
method
I would assume the coupon is not a string that looks like JSON, but looks like one when you print it. Try decoding it's string representation rather. I'm not sure of the code for this(I'm not a RoR programmer) but I've seen many similar situations to the one you're in...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.