0

I'm facing a weird problem, I am using Ruby JSON to parse a string but failed to do so. I am getting a weird character in near the beginning of the string which only shows up in C9 Editor or JSLINT website. I can neither copy or paste this character.

I am attaching a screenshot of the same.JSLINT SCREENSHOT

How Can I Parse this string as I get unexpected token error. Also can someone tell what this character is?

update: Trying to paste the string here. bar = "{\"id\":3710}"

Nikhil
  • 1,268
  • 2
  • 13
  • 29
  • 2
    Can you post the actual JSON? We can't tell why JSON.Parse(bar) fails if we don't know what `bar` is. You can obfuscate any sensitive content. – whodini9 Dec 22 '16 at 19:12
  • I suspect you have Byte Order Mark in the begining of your string. You can try strip it like described here [Is there a way to remove the BOM from a UTF-8 encoded file?](http://stackoverflow.com/questions/5011504/is-there-a-way-to-remove-the-bom-from-a-utf-8-encoded-file) – Alexander Yanyshin Dec 22 '16 at 19:13
  • Also, if your string doesn't work copied directly into JSONLint, why should it work in JSON.parse() ? Its indeed likely to be a special character as @yanys stated. – whodini9 Dec 22 '16 at 19:15
  • @whodini9 I have pasted the string here and it seems to be reproducing the problem stated. – Nikhil Dec 22 '16 at 19:28
  • @yanys the string is coming from Woocommerce API and I am using HTTParty and in turn JSON to parse it. Is there a way to ignore the BOM character there? – Nikhil Dec 22 '16 at 19:32
  • @Nikhil you can try something like `json_response.sub!(/^\xEF\xBB\xBF/, '')` to strip BOM from response string. – Alexander Yanyshin Dec 22 '16 at 19:47
  • @yanys I also solved the issue by passing the HTTParty response as JSON.parse as `JSON.parse(bar.encode('utf-8', 'binary', :undef => :replace, :replace => ''))` – Nikhil Dec 23 '16 at 09:30

0 Answers0