3

A simple question:

In rails I get as response an hash like this:

{"success":true,"new_id":816704027}

So, the difference from a normal structure I guess is- "new_id": -instead of- new_id:

Does anyone know how to retrieve the data labelled "new_id"? The usual array["new_id"] doesn't work.

The response to the code:

new_customer_id = @response.body
puts new_customer_id
puts new_customer_id["new_id"]

is simply:

=> {"success":true,"new_id":816704028}
=> new_id

I come from the implementation of JSON_response. Anyway, they changed the app and I don't have anymore a JSON message, but they use the method:

return_200(additional_items: {:new_id => "@customer.id"} )

More:

If I write:

new_customer_id = @response.body
puts new_customer_id
puts new_customer_id[:new_id]

the answer printed is simply:

=> {"success":true,"new_id":816704028}

and the request for the :new_id content does not to be received.

Much more interesting is the following: After the fact that:

puts new_customer_id["new_id"]

prints:

=> new_id

If I write:

puts new_customer_id["new_id"][0]
puts new_customer_id["new_id"][1]
puts new_customer_id["new_id"][2]
...

I obtain:

=> n
=> e
=> w
...

Also:

if I write:

puts new_customer_id["new_"]
puts new_customer_id["new_i"]

I obtain:

=> new_
=> new_i

and if I write:

puts new_customer_id["new_id_anyOtherCharacter"]

I get nothing

Luca

7
  • Why doesn't array["new_id"] work? what errors do you get when using it? Commented Jun 1, 2015 at 11:32
  • Try this in Rails Console a = ActiveSupport::JSON.decode('{"success":true,"new_id":816704027}') then access it using a["new_id"] Commented Jun 1, 2015 at 11:33
  • @TamerShlash, the answer to your question is below Commented Jun 1, 2015 at 11:50
  • @LucaDanieli Please edit your question to add details, rather than adding them as an answer. Commented Jun 1, 2015 at 11:52
  • 1
    What is the result of new_customer_id.class? Commented Jun 1, 2015 at 12:18

3 Answers 3

2

That's not a ruby object you are getting back. It's JSON. You can get the new_id in a variety of ways:

JSON.parse(@response.body)["new_id"]

JSON.parse(@response.body).symbolize_keys[:new_id]

JSON.parse(@response.body).with_indifferent_access[:new_id]

Sign up to request clarification or add additional context in comments.

Comments

0

I bet the hash has a symbol key instead of a string key. Try with array[:new_id].

1 Comment

that doesn't work. I don't know why but it doesn't give me any answer. I updated the question, so you can read what happens
0

use params to get the value like:

new_id= array[:new_id]

2 Comments

He clearly said a "response" hash, not a params hash.
I have just changed the answer, please undo your down voting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.