0

I have the following function in my controller file where I have a string and I am trying to parse it using JSON.parse. The problem I am facing is that I am not able to print the value of message which is present in the hash that is being returned.

def index ......... r = '{"response":"{\"message\":\"The following page was successfully Created 3035\",\"success\":\"0\",\"page_id\":\"3035\"}"}' @hash = JSON.parse(r) respond_to do |format| format.html end end 

In my view file I am using the following code

<%= @hash['response']['message'] %> 

The output that I am getting is message instead of getting The following page was successfully Created 3035

I have 'require json' on my controller file.

If I do

<%= @hash['response'] %> 

Then I get the whole hash.Please help

4
  • From where did you get the r value ?
    – Siva
    CommentedOct 7, 2013 at 10:56
  • The r value is something which I want to check.The value of r would be dynamic but right now just to check how it will work I am assigning a string in JSON format.CommentedOct 7, 2013 at 11:00
  • But the JSON is invalid thats why its not working.Try with proper JSON
    – Siva
    CommentedOct 7, 2013 at 11:02
  • @shiva: yes you are correct..The JSON string was incorrect.CommentedOct 7, 2013 at 11:05

3 Answers 3

2

The JSON string looks off. It basically contains a single key/value pair, with the key being response and the rest being a String containing what looks to be escaped JSON:

"{\"message\":\"The following page was successfully Created 3035\",\"success\":\"0\",\"page_id\":\"3035\"}" 

In other words, the behavior you're seeing is to be expected given the input you're giving.

If you change the JSON input to (ie making sure the value in response isn't given as a JSON-encoded string):

r = '{"response":{"message":"The following page was successfully Created 3035","success":"0","page_id":"3035"}}' 

I imagine it'll work like you expect.

The reason @hash['response']['message'] is returning "message" is because @hash['response'] is a String. Sending [] to a String with a String parameter results in the parameter String being returned if it occurs in the recipient String:

"foobar"["bar"] #=> "bar" "foobar"["baz"] #=> nil 

See String#[] for the details.

0
    0

    It seems that you have wrong JSON-string assigned to r. The proper string should look like this:

    "{\"response\":{\"message\":\"The following page was successfully Created 3035\",\"success\":\"0\",\"page_id\":\"3035\"}}" 

    In comparison to your version it does not have double quotes around response contents, so that JSON.parse returns you a hash with proper values you are expecting:

    {"response"=>{"message"=>"The following page was successfully Created 3035", "success"=>"0", "page_id"=>"3035"}} 
    1
    • Thanks Hck for the answer . You mentioned it correctly but I found Jakob's answer more informative so I have accepted his answer as the correct one.CommentedOct 7, 2013 at 11:11
    0

    Try this:-

    def index r = '{"response":"{\"message\":\"The following page was successfully Created 3035\",\"success\":\"0\",\"page_id\":\"3035\"}"}' @hash = JSON.parse(r) @messagehash= JSON.parse(@hash['response') respond_to do |format| format.html end end 

    In your View:-

     <%= @messagehash['message'] %> 

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.