Reputation: 5462
I have the following JSON returned in a response:
"{\n \"notices\": [\n {\n \"id\": \"1234\",\n \"projectId\": 1,\n \"groupId\": \"55\",\n \"createdAt\": \"2014-10-22T03:00:00.407Z\"\n },\n {\n \"id\": \"1234\",\n \"projectId\": 1,\n \"groupId\": \"55\",\n \"createdAt\": \"2014-10-22T03:00:00.407Z\"\n },\n ],\n \"count\": 2\n}\n"
In irb for Ruby 2.3.0, when I use JSON.parse(x)
on the above response, I get:
JSON::ParserError: 419: unexpected token at '],
"count": 2
}'
even though http://jsonlint.com says it is valid JSON. What am I doing wrong?
Upvotes: 1
Views: 678
Reputation: 16793
There seems to be an issue with having a comma after the last object in your array:
\"createdAt\": \"2014-10-22T03:00:00.407Z\"\n },\n ],\n \"count\": 2\n}\n"
^ This comma
After getting rid of it, I got the parse to work as expected:
JSON.parse("{\n \"notices\": [\n {\n \"id\": \"1234\",\n \"projectId\": 1,\n \"groupId\": \"55\",\n \"createdAt\": \"2014-10-22T03:00:00.407Z\"\n },\n {\n \"id\": \"1234\",\n \"projectId\": 1,\n \"groupId\": \"55\",\n \"createdAt\": \"2014-10-22T03:00:00.407Z\"\n } ],\n \"count\": 2\n}\n")
=> {"notices"=>[{"id"=>"1234", "projectId"=>1, "groupId"=>"55", "createdAt"=>"2014-10-22T03:00:00.407Z"}, {"id"=>"1234", "projectId"=>1, "groupId"=>"55", "createdAt"=>"2014-10-22T03:00:00.407Z"}], "count"=>2}
Upvotes: 2