1
data = {
  "CEO": "William Hummel",
  "CFO": "Carla Work"
  }

I'm trying to parse the json data above with JSON.parse(data) in IRC, but it won't work.

I'm getting the following error: "SyntaxError: (irb):44: syntax error, unexpected ':', expecting tASSOC"

Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
  • Maybe this can help: http://stackoverflow.com/questions/1826727/how-do-i-parse-json-with-ruby-on-rails – Tony Aug 05 '14 at 18:09
  • Yes, it's exactly that most upvoted answer I'm trying to use: http://stackoverflow.com/a/11430924/3205492 – Fellow Stranger Aug 05 '14 at 18:10
  • Well then, I believe you want data to look like `data = '{...}'` Parse is looking for a string, and you are passing a dict. – Tony Aug 05 '14 at 18:13
  • 1
    unlike javascript, json is not first class citizen of Ruby. json data is represented as a string. You can use %q() notation for multiline strings. – nishu Aug 05 '14 at 18:17

1 Answers1

3

JSON.parse takes a string argument. You are trying to construct a Hash using JSON syntax. Use a string instead:

data = '{"CEO": "William Hummel", "CFO": "Carla Work"}'
JSON.parse(data)
infused
  • 24,000
  • 13
  • 68
  • 78