0

I'm reading a CSV file. It contains a column as

{"doctype"=>"birthrecord", "records"=>[{"pagenum"=>"5", "recordId"=>"7", "tagGroups"=>[{"data"=>{"first"=>"given_name", "given_name"=>"Severiano ", "surname"=>"Bustamante"}}]}]}

The header of this columns is Output. When I do row["Output"] its returning sting of hash as

"{"doctype"=>"birthrecord", "records"=>[{"pagenum"=>"5", "recordId"=>"7", "tagGroups"=>[{"data"=>{"first"=>"given_name", "given_name"=>"Severiano ", "surname"=>"Bustamante"}}]}]}"

How can I access the hash like normal hash?

Can anyone help me please.

  • 2
    I know if I say `eval`, I'm going to get stoned to death (for a good reason), but... how desperate are you and how much do you trust the source? – John Dvorak Feb 22 '14 at 18:30
  • possible duplicate of [parsing in ruby](http://stackoverflow.com/questions/5608211/parsing-in-ruby) – John Dvorak Feb 22 '14 at 18:32
  • @JanDvorak, is it right way to use eval there? it works but... – user2986573 Feb 22 '14 at 18:36
  • Most likely not. The link I've posted looks relevant, though. Where is your string coming from? – John Dvorak Feb 22 '14 at 18:39
  • 1
    There appear to be a few viable options outlined in this thread: http://stackoverflow.com/questions/1667630/how-do-i-convert-a-string-object-into-a-hash-object – pdxdev Feb 22 '14 at 18:41
  • 1
    We need to see your code and a sample of your input data. The "column" isn't CSV format, and instead appears to be a hash definition. – the Tin Man Feb 22 '14 at 20:37

1 Answers1

1

The dataset looks almost like JSON except => is used in place of :, so replacing them you can now adequately parse it like a JSON object

JSON.parse('{"doctype"=>"birthrecord", "records"=>[{"pagenum"=>"5", "recordId"=>"7", "tagGroups"=>[{"data"=>{"first"=>"given_name", "given_name"=>"Severiano ", "surname"=>"Bustamante"}}]}]}'.gsub("=>", ":"))["records"][0]["tagGroups"][0]["data"]
#=> {"first"=>"given_name", "given_name"=>"Severiano ", "surname"=>"Bustamante"}
bjhaid
  • 9,592
  • 2
  • 37
  • 47
  • The OP needs to clarify the input and show where the data displayed is coming from. It's not a CSV formatted record or field. – the Tin Man Feb 22 '14 at 20:39