Ruby on Rails JSON.parse unexpected token error
Well, we can only answer based on the part of the JSON you showed us, but it has two problems:
- All the quote characters (
"
) are escaped; they don't need to be unless they are used in a double-qoted string, which it appears they are not. - You are missing a closing brace (
}
).
Otherwise it passes based on https://jsonlint.com/.
In my case it was a hidden tab character which showed up only when i pasted it into vim.
You can directly use JSON.Parse()
in-build method:
content = "[{\"addon_id\":\"1\",\"addon_price\":\"5\"}]"# OUTPUT at Console => "[{\"addon_id\":\"1\",\"addon_price\":\"5\"}]"JSON.parse(content.gsub('\"', '"'))# OUTPUT at Console => [{"addon_id"=>"1", "addon_price"=>"5"}]