1

I have a string

string = '{"a" => [{"b" => 2}]}'

eval(string) 
# => {"a" => [{"b" => 2}]}

I need alternative for this to have output like {"a" => [{"b" => 2}]}

MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
vishnu vardhan
  • 129
  • 1
  • 9
  • 3
    Alternative 1) you could build a limited ruby parser that can only parse hashes, arrays, and a few primitives. Or alternative 2) with a few string substitutions, make that string a json string and use `JSON.parse`. – Sergio Tulentsev Aug 17 '17 at 17:42
  • 1
    where does that string come from? Do you have control over that data? Is it stored in your database? – MrYoshiji Aug 17 '17 at 17:45
  • JSON.parse is not working – vishnu vardhan Aug 17 '17 at 17:50
  • @Yoshiji I added {"a" => [{"b" => 2}]} in excel sheet. While I read it, it comes like '{"a" => [{"b" => 2}]}' – vishnu vardhan Aug 17 '17 at 17:51
  • @vishnuvardhan where do you get this `string`? I assume that you are reading this from an excel file as you just have said? I agree on Sergio's alternative solution to make use of JSON-string instead of Hash-string. Therefore, you can just simply save on the excel file the JSON representation of the string: i.e. `file << your_hash.to_json`, then parse the JSON string afterwards like so: `your_hash = JSON.parse(file)` – Jay-Ar Polidario Aug 17 '17 at 17:59
  • While the accepted answer to [How do I convert a String object into a Hash object](https://stackoverflow.com/questions/1667630/how-do-i-convert-a-string-object-into-a-hash-object) is to use `eval`, there are 10 other answers that don't use `eval` – Simple Lime Aug 17 '17 at 18:04
  • @vishnuvardhan: "JSON.parse is not working " - did you notice the "few string substitutions" part? – Sergio Tulentsev Aug 17 '17 at 18:04
  • Possible duplicate of [How to convert string to hash WITHOUT EVAL](https://stackoverflow.com/questions/26457354/how-to-convert-string-to-hash-without-eval) – Simple Lime Aug 17 '17 at 19:19
  • Its duplicate. I know. But it didn't find solution in many other questions – vishnu vardhan Aug 17 '17 at 22:20

1 Answers1

1

When storing data in strings that will be parsed programmatically, it's best to format those strings using a standardized data-interchange format, such as JSON. Your string, formatted into JSON, would look like this:

{"a": [{"b": 2}]}

If you have any control over how the data is saved in excel, you should make sure it's saved in JSON format like this. If, for some reason, you're not allowed to modify the format of the data in excel, your next best option is to convert it to JSON before parsing it.

Fortunately for you, the data is already very similar to JSON. The only difference is that JSON uses : instead of =>, so you can do this:

require "json"
string = '{"a" => [{"b" => 2}]}'.gsub("=>", ":")
data = JSON.parse string
p data # => {"a" => [{"b" => 2}]}
eiko
  • 5,110
  • 6
  • 17
  • 35
  • 2
    Yep, if there are no string values, this regex should work fine. If, however, there are string values and they contain `=>`.... :) – Sergio Tulentsev Aug 17 '17 at 18:07
  • @SergioTulentsev true, and I don't think a regex can ultimately account for the recursive nature of a "hash-like string inside a hash." But the answer may be sufficient if the OP's hash-strings are guaranteed to have simple key-value pairs (maybe only integers, only alphanumeric strings, etc). – eiko Aug 17 '17 at 18:14
  • Agree with that. – Sergio Tulentsev Aug 17 '17 at 18:15
  • I won't be having format other this. I think it works.Thanks – vishnu vardhan Aug 17 '17 at 22:19