0

I have string like this

a="{\"company\"=>\"khkhkh\", \"email\"=>\"[email protected]\",\"address\"=>\"yiyiyu\", \"date\"=>Mon, 28 Apr 2014 13:14:10 BST +01:00}"

but i have made a mistake when generating this string. that is, i appended the date without converting to string. So now if i try to get back this into a hash using eval(a) . This throws a error, with is acceptable.

SyntaxError: (eval):1: syntax error, unexpected tCONSTANT, expecting => ....

Is there any way to get that hash back, since iam in a situation that i cant regenerate this string.

Any help would be highly appreciable. Thanks.

7
  • what is the problem with adding quotes manually? Commented Jan 7, 2016 at 13:59
  • Actually there is problem in data, \"date\"=>Mon, 28 Apr 2014 13:14:10 BST +01:00. Date also should be in "" (double commas), after that use eval Commented Jan 7, 2016 at 14:01
  • a="{\"company\"=>\"khkhkh\", \"email\"=>\"[email protected]\",\"address\"=>\"yiyiyu\", \"date\"=>'Mon, 28 Apr 2014 13:14:10 BST +01:00'}" Commented Jan 7, 2016 at 14:02
  • eval(a) => {"company"=>"khkhkh", "email"=>"[email protected]", "address"=>"yiyiyu", "date"=>"Mon, 28 Apr 2014 13:14:10 BST +01:00"} Commented Jan 7, 2016 at 14:03
  • @gasanov this string i have saved this generated string in more than 2.5 lakhs of rows in rails db. so changing that is a difficult process Commented Jan 7, 2016 at 14:07

3 Answers 3

2

For your immediate predicament:

a.gsub(/\"date\"\s*=>(.*?)(\}|,\s*\")/, '"date"=>"\1"\2')

This should work even if the date is not the last entry of the hash.

For the next time around: It is really not a good idea to serialize data by manually turning them into code that you eval later. In most cases your best bet is probably to just generate JSON and parse that later. Using a proper JSON serializer/generator will also make sure your data is syntactically correct.

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

1 Comment

Thanks. That worked for me.. Yeah you are right.. but this i did when i didn't know much about JSON formatting.. From now onwards, i might choose the JSON way.. :)
1

If the date is always last you could go with the straightforward approach:

date = a.gsub(/[\{\}]/, '').split('=>').last
a.gsub(date, "\"#{date}\"")

Will return

"{\"company\"=>\"khkhkh\", \"email\"=>\"[email protected]\",\"address\"=>\"yiyiyu\", \"date\"=>\"Mon, 28 Apr 2014 13:14:10 BST +01:00\"}"

Comments

1

If you have multiple dates, try to replace them with quoted dates by regexp:

wrong_data = "" # your string
correct_data = wrong_data.gsub(/(\w{3}, \d{2} \w{3} \d{4} \d{2}:\d{2}:\d{2} \w{3} \+\d{2}:\d{2})/, '"\1"')
eval(correct_data)

PS. As @Gene truly noticed, you should avoid using eval in your code.

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.