I have been writing some scripts in ruby to parse JSON generated data. To do this it requires the json ruby gem which works great to parse through JSON output. Anyhow I was loading files into ruby and parsing them without issue until i got to a larger file and I received the below error.
Error:
/usr/local/lib/ruby/gems/1.8/gems/json-1.1.3/lib/json/common.rb:122:in `parse’: 574: unexpected token at ” (JSON::ParserError)
from /usr/local/lib/ruby/gems/1.8/gems/json-1.1.3/lib/json/common.rb:122:in `parse’
from tickets.rb:13
from tickets.rb:11:in `open’
from tickets.rb:11
The file was about 650 lines long and included a bunch of database output to JSON. Some of the fields had paragraphs of data so I assumed that the issue would be with a random quote or tick that hadn’t been back slashed. So what I started to do was add the text BREAK every 100 lines to see if the parse error above would show up or the BREAK would cause the error. Anyhow I got all the way down to the last 25 lines and realized exactly what the error was. There was a blank line at the bottom of the file that JSON was attempting to parse but didn’t know how. Once I removed the blank line at the bottom of the file it was able to be parsed without issue.
Resolution:
Remove blank line at the end of the file.
Check out the below books on JSON and JavaScript to up your skills!