Reputation: 93
I have this object with data that I want to access:
[#<ConceptPayment id: nil, amount: 1, price: 1000.0, concept_id: 3, concept_type: "RegisterType", created_at: nil, updated_at: nil, transaction_sara_id: nil, code: nil, date_code: nil, discount_amount: nil, type_discount_id: nil, observations: nil>]
I tried to parse it to a json with to_json
:
"[{\"id\":null,\"amount\":1,\"price\":1000.0,\"concept_id\":3,\"concept_type\":\"RegisterType\",\"created_at\":null,\"updated_at\":null,\"transaction_sara_id\":null,\"code\":null,\"date_code\":null,\"discount_amount\":null,\"type_discount_id\":null,\"observations\":null}]"
then I did this JSON.parse
[{"id"=>nil, "amount"=>1, "price"=>1000.0, "concept_id"=>3, "concept_type"=>"RegisterType", "created_at"=>nil, "updated_at"=>nil, "transaction_sara_id"=>nil, "code"=>nil, "date_code"=>nil, "discount_amount"=>nil, "type_discount_id"=>nil, "observations"=>nil}]
but I can not access the data, I just want to achieve this data[:price]
How can I get access to the data? What is the way to convert an object or access the data of a json?
Upvotes: 0
Views: 785
Reputation: 658
NOTE: Assuming you are storing the ConceptPayment
object as cp_obj
:
I think what you need is deep_symbolize_keys
. Your question does not detail what kind of object ConceptPayment is.
If it is a Model object, the following should work:
as_json
to get the object as a hash. Try cp_obj.as_json
. It normally gets you the object in JSON format (Rails 5.2). deep_symbolize_keys
, like so: my_hash = cp_obj.as_json.deep_symbolize_keys
and then you should be able to do my_hash[:price]
. Example (I just ran this on my Rails console against a User
class that I have):
irb(main):002:0> user = User.find 6
User Load (7.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 6], ["LIMIT", 1]]
=> #<User id: 6, full_name: "Geeks", email: "[email protected]", created_at: "2019-02-04 06:59:33", updated_at: "2019-02-04 06:59:33">
irb(main):003:0> user.as_json
=> {"id"=>6, "full_name"=>"Geeks", "email"=>"[email protected]", "created_at"=>Mon, 04 Feb 2019 12:29:33 IST +05:30, "updated_at"=>Mon, 04 Feb 2019 12:29:33 IST +05:30}
irb(main):004:0> my_hash = user.as_json.deep_symbolize_keys
=> {:id=>6, :full_name=>"Geeks", :email=>"[email protected]", :created_at=>Mon, 04 Feb 2019 12:29:33 IST +05:30, :updated_at=>Mon, 04 Feb 2019 12:29:33 IST +05:30}
irb(main):005:0> my_hash[:full_name]
=> "Geeks"
If it is not a model object, then I believe JSON.parse(cp_obj.to_json).deep_symbolize_keys
should work (as you are able to get JSON.parse to work already)!
Example:
irb(main):011:0> JSON.parse('{"name":"vaibhav", "more" : {"details": "balding guy! :P"}}').deep_symbolize_keys[:more][:details]
=> "balding guy! :P"
I hope that helps.
Upvotes: 0
Reputation: 15258
In your code keys are String
. But you try to find value with Symbol
key.
In this case you have to use symbolize_names
option.
string = "[{\"id\":null,\"amount\":1,\"price\":1000.0,\"concept_id\":3,\"concept_type\":\"RegisterType\",\"created_at\":null,\"updated_at\":null,\"transaction_sara_id\":null,\"code\":null,\"date_code\":null,\"discount_amount\":null,\"type_discount_id\":null,\"observations\":null}]"
data = JSON.parse(string, symbolize_names: true).first
data[:price] #=> 1000.0
Upvotes: 0
Reputation: 13
Have you tried data[0]['price']
or data['price']
?
It looks like something an activerecord query would return. In that case you can just assign the query result to let's say 'data' and access each member with data['member_name']
unless the query return multiple items in that case you would have to specify the index first e.g. data[index_number]['member_name']
I believe you could also do something like data.first.price
or data.price
Rails 4: how to access an attribute of an ActiveRecord_Relation-Object?
Hope this helps.
Upvotes: 1