ruby - how can I acess elements of a JSON retunr from a Stripe api call? -


i using ruby , sinatra. after stripe api call, want access element, json return, , put database.

the ruby code is:

require 'sinatra'

require 'stripe' require 'pg' require 'sequel'  '/save_customer' customer = stripe::customer.retrieve("cus_6efjsbj8gctxxx") puts customer last4 = customer["sources"]["data"]["last4"] db[:stripe_customers].insert(:user_id=>user_id, :email=>email, :stripe_customer_id=>customer_id, :stripe_customer_card=> last4) end 

the json (taken api docs, not return) follows:

{   "object": "customer",   "created": 1431570089,   "id": "cus_6efjsbj8gctxxx",   "livemode": false,   "description": "example customer",   "email": null,   "delinquent": false,   "metadata": {   },   "subscriptions": {     "object": "list",     "total_count": 0,     "has_more": false,     "url": "/v1/customers/cus_6efjsbj8gctmrd/subscriptions",     "data": [      ]   },   "discount": null,   "account_balance": 0,   "currency": "usd",   "sources": {     "object": "list",     "total_count": 1,     "has_more": false,     "url": "/v1/customers/cus_6efjsbj8gctmrd/sources",     "data": [       {         "id": "card_162byrbvd5ndd62kfaoncg4g",         "object": "card",         "last4": "4242",         "brand": "visa",         "funding": "credit",         "exp_month": 12,         "exp_year": 2018,         "country": "us",         "name": null,         "address_line1": null,         "address_line2": null,         "address_city": null,         "address_state": null,         "address_zip": "123456",         "address_country": null,         "cvc_check": "pass",         "address_line1_check": null,         "address_zip_check": "pass",         "dynamic_last4": null,         "metadata": {         },         "customer": "cus_6efjsbj8gctxxx"       } 

i have tried:

last4 = customer[:data][:sources][:last4] 

and "" above.

th error message has varied, typeerror - no implicit conversion of string integer. asusme becasue have not extracted element needed correctly, or mean api call did not work assume did.

can please?

thanks always.

try this:

last4 = customer["sources"]["data"][0]["last4"] 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -