0

I have a string like below :

"[a06aad57-5671-482e-dbdd81dc39b1]   Parameters: {\"ToCountry\"=>\"US\", \"ToState\"=>\"AL\", \"SmsMessageSid\"=>\"SMa1a9e32a7503f7342b7065d77174d\"}"  

I would like to capture only value of 'Parameters:' as below and convert it to hash. Key/value can be any value in above raw string :

{"ToCountry"=>"US", "ToState"=>"AL", "SmsMessageSid"=>"SMa1a9e32a7503f3767342b7065d77174d"}

2 Answers 2

2

You can do it through a few steps:

require 'json'

string = "[a06aad57-5671-482e-dbdd81dc39b1]   Parameters: {\"ToCountry\"=>\"US\", \"ToState\"=>\"AL\", \"SmsMessageSid\"=>\"SMa1a9e32a7503f7342b7065d77174d\"}"
prepared_string = string.match(/Parameters:(.*)/)[1].gsub("=>", ":")
json = JSON.parse(prepared_string)
#=> {"ToCountry"=>"US", "ToState"=>"AL", "SmsMessageSid"=>"SMa1a9e32a7503f7342b7065d77174d"}
Sign up to request clarification or add additional context in comments.

Comments

0

Not via regex and not the best approach evaling a user input but this will do it:

eval str.split("Parameters: ")[1]

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.