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"}

code_aks
  • 1,972
  • 1
  • 12
  • 28
roarfromror
  • 276
  • 1
  • 2
  • 11

2 Answers2

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"}
Oleksandr Holubenko
  • 4,310
  • 2
  • 14
  • 28
0

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

eval str.split("Parameters: ")[1]
Alok Swain
  • 6,409
  • 5
  • 36
  • 57