Question

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\"}")
Was it helpful?

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.

OTHER TIPS

Here is a simple answer to convert a PG Hstore string (data) into a javascript object :

JSON.parse( data.replace(/=>/g,':') )

or

JSON.parse( data.replace(/"/g,'"').replace(/=>/g,':') )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top