0
"{\"status\":1,\"redirect\":\"/some/uri/uri2/index.html?post_login=80607979823520\",\"security_token\":\"/cpsess8233434446\"}"

I am getting this response as string and I need to extract security_token value. I tried to convert the string to Hash by eval method.seems not worked and I need to do a regex match.

3 Answers 3

2

You can do this:

require 'json'
a =  JSON.load "{\"status\":1,\"redirect\":\"/some/uri/uri2/index.html?post_login=80607979823520\",\"security_token\":\"/cpsess8233434446\"}"
p a["security_token"]  #=> "/cpsess8233434446"
Sign up to request clarification or add additional context in comments.

Comments

2

You need to parse the JSON data..

result = "{\"status\":1,\"redirect\":\"/some/uri/uri2/index.html?post_login=80607979823520\",\"security_token\":\"/cpsess8233434446\"}" 
h = JSON.parse(result)
h['security_token']      # => "/cpsess8233434446"

Comments

1

You can either JSON.load the data and filter for ['security_token'] or use a .match(/security_token/) style regex expression.

I'd suggest the prior for future readability and code maintenance.

Comments

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.