26

I am trying to parse JSON data in my Rails 3 application with JSON.parse. I keep getting this error:

737: unexpected token at '{\"0\":{\"class\":\"window\",\"text\":\"Testing\",\"style\":\"position: absolute; top: 8px; left: 8px; width: 560px; height: 290px; z-index: 0; \"}

The actual JSON is a lot longer, but it is basically the same.

3 Answers 3

43

Well, we can only answer based on the part of the JSON you showed us, but it has two problems:

  1. 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.
  2. You are missing a closing brace (}).

Otherwise it passes based on https://jsonlint.com/.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! The problem was the escaped quotes. This worked: JSON.parse(content.gsub('\"', '"'))
Escaping is hell: I had the same problem and this helped me. stackoverflow.com/questions/3066886/…
14

In my case it was a hidden tab character which showed up only when i pasted it into vim.

2 Comments

The same in my case. Had some whitespace around the object
Ditto. I had some unescaped \r characters in a field value.
2

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"}]

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.