Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MultiJson::DecodeError unexpected token at {"email":"[email protected]"}

I'm developing a new Ruby on Rails 3.2 application. This application will receive a periodic json callback with statistics. The callback i receive is not entirely valid. The json rules are separated by newlines.

The callback POSTs have a content-type header of application/json, and contain exactly one JSON string per line, with each line representing one event. Please note that currently the POST headers define this post as application/json, though it’s not; each line is a valid JSON string, but the overall POST body is not. For example:

This is a example of the callback:

{"email":"[email protected]","timestamp":1322000095,"unique_arg":"my unique arg","event":"delivered"}
{"email":"[email protected]","timestamp":1322000097,"unique_arg":"my unique arg","event":"click"}
{"email":"[email protected]","timestamp":1322000096,"unique_arg":"my unique arg","event":"open"}

When i receive this callback my Rails application crashes with a "MultiJson::DecodeError"

743: unexpected token at '{"email":"[email protected]","timestamp":1322000096,"unique_arg":"my unique arg","event":"open"}'

I think the application detects the application/json header and try automatic to parse it. How can i convert this to a valid JSON object, so i can use this in my controller?

Thanks.

like image 304
Mike de Bock Avatar asked Dec 30 '25 11:12

Mike de Bock


1 Answers

Put gem 'yajl-ruby' in Gemfile and require 'yajl/json_gem' config/application.rb


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!