Search code examples
rubyjsonruby-on-rails-4hstore

JSON parse error hstore serialization rails 4


Could someone point out where the JSON error is in the following...

"{\"a\"=>\"b\"}"

I get the follow error when doing JSON.parse

JSON::ParserError: 757: unexpected token at '{"a"=>"b"}'

Thanks

UPDATE

Yep, as the commenter points out, I can't parse it bc it is a Ruby hash literal. FWIW I was dealing with this value when trying to do nested hashes inside of PG Hstore. Turns out, you shouldn't really do that / that is not what Hstore is currently designed to support. If you wanted to stick with this approach you can do the following to get the hash value:

eval("{\"a\"=>\"b\"}")

Solution

  • That's not JSON. If I assume the outer quotes and the escapes on the inner quotes are not really in the data, that's:

    {"a"=>"b"}
    

    JSON doesn't use =>. In JSON it would be

    {"a":"b"}
    

    Edit: Ah, @falsetru says in a comment that what you have there is a Ruby Hash literal. I'm not a Ruby guy.