1

When I try to parse the following JSON in ruby (or at http://json.parser.online.fr/) I get an error, but I can't figure out why.

JSON to parse:

"{\"RED\"=>{\"t1\"=>0, \"t2\"=>2},
  \"DANGEROUS_SITUATION\"=>{\"t1\"=>28, \"t2\"=>33},
  \"SUB\"=>{\"t1\"=>1, \"t2\"=>0}, \"RED_CARS\"=>{\"t1\"=>0, \"t2\"=>0},
  \"CRASH\"=>{\"t1\"=>10, \"t2\"=>6}, \"TRICKS\"=>{\"t1\"=>2,
  \"t2\"=>3}, \"FREE_TRIES\"=>{\"t1\"=>9, \"t2\"=>5},
  \"OWNERS\"=>{\"t1\"=>48, \"t2\"=>52}, \"ERRORS\"=>{\"t1\"=>5,
  \"t2\"=>9}, \"YELLOW_CARS\"=>{\"t1\"=>0, \"t2\"=>0},
  \"FANS\"=>{\"t1\"=>40, \"t2\"=>41}}"

Edit: as ceommented below, this is not Json, but a stringified Ruby hash, is there a way to convert this in to a Ruby hash?

4
  • This is not JSON. This is stringified ruby hash. As such, it can't be parsed (with json parser) Commented Jul 21, 2016 at 10:04
  • Okay, is there a way to convert this back in to a ruby hash? Commented Jul 21, 2016 at 10:08
  • There are several ways, but you better fix the thing that produced this content, and make it emit proper json. JSON is a valid data exchange format. Stringified ruby hashes are not. Commented Jul 21, 2016 at 10:09
  • True, I managed to find the source of this and changed it, this fixed the issue thanks Commented Jul 21, 2016 at 10:16

2 Answers 2

2

The most secure way for you to do this would be to gsub the rockets for colons and then parse the string i.e.

stringified_hash = "{\"RED\"=>{\"t1\"=>0, \"t2\"=>2}, \"DANGEROUS_SITUATION\"=>{\"t1\"=>28, \"t2\"=>33}, \"SUB\"=>{\"t1\"=>1, \"t2\"=>0}, \"RED_CARS\"=>{\"t1\"=>0, \"t2\"=>0}, \"CRASH\"=>{\"t1\"=>10, \"t2\"=>6}, \"TRICKS\"=>{\"t1\"=>2, \"t2\"=>3}, \"FREE_TRIES\"=>{\"t1\"=>9, \"t2\"=>5}, \"OWNERS\"=>{\"t1\"=>48, \"t2\"=>52}, \"ERRORS\"=>{\"t1\"=>5, \"t2\"=>9}, \"YELLOW_CARS\"=>{\"t1\"=>0, \"t2\"=>0}, \"FANS\"=>{\"t1\"=>40, \"t2\"=>41}}"
JSON.parse(stringified_hash.gsub('=>', ':')

You can also eval to return the stringified_hash back to a Hash and then call .to_json but that's open to attacks if there is any evaluatable code in the string

json = eval("{\"RED\"=>{\"t1\"=>0, \"t2\"=>2}, \"DANGEROUS_SITUATION\"=>{\"t1\"=>28, \"t2\"=>33}, \"SUB\"=>{\"t1\"=>1, \"t2\"=>0}, \"RED_CARS\"=>{\"t1\"=>0, \"t2\"=>0}, \"CRASH\"=>{\"t1\"=>10, \"t2\"=>6}, \"TRICKS\"=>{\"t1\"=>2, \"t2\"=>3}, \"FREE_TRIES\"=>{\"t1\"=>9, \"t2\"=>5}, \"OWNERS\"=>{\"t1\"=>48, \"t2\"=>52}, \"ERRORS\"=>{\"t1\"=>5, \"t2\"=>9}, \"YELLOW_CARS\"=>{\"t1\"=>0, \"t2\"=>0}, \"FANS\"=>{\"t1\"=>40, \"t2\"=>41}}").to_json
Sign up to request clarification or add additional context in comments.

Comments

0

The expected Json format is rather like that :

{
 "RED":{"t1":0, "t2":2}, 
 "DANGEROUS_SITUATION":{"t1":28, "t2":33}, 
 "SUB":{"t1":1, "t2":0}, 
 "RED_CARS":{"t1":0, "t2":0}
}

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.