JSON::Parsererror: 757: Unexpected Token at '{

JSON::ParserError: 757: unexpected token at '{

What you have is a hash printed as String. To convert it into a Hash use eval.

ch = "{\"report_name\"=>\"Study/Control: ABIRATERONE ACETATE - 20151413355\", \"left_mue_start_date\"=>\"02-26-2015\", \"left_mue_end_date\"=>\"03-19-2015\", \"right_mue_start_date\"=>\"02-26-2015\", \"right_mue_end_date\"=>\"03-19-2015\", \"report_formulary_id\"=>\",7581\", \"mue\"=>\"true\", \"mue_type\"=>\"study/control\", \"chain_id\"=>\"1\", \"left_mue_formulary_ids\"=>[\"7581\"], \"action\"=>\"create_report\", \"controller\"=>\"informatics\", \"user_id\"=>339}"
hash = eval(ch)
# => {"report_name"=>"Study/Control: ABIRATERONE ACETATE - 20151413355", "left_mue_start_date"=>"02-26-2015", "left_mue_end_date"=>"03-19-2015", "right_mue_start_date"=>"02-26-2015", "right_mue_end_date"=>"03-19-2015", "report_formulary_id"=>",7581", "mue"=>"true", "mue_type"=>"study/control", "chain_id"=>"1", "left_mue_formulary_ids"=>["7581"], "action"=>"create_report", "controller"=>"informatics", "user_id"=>339}

PS: A JSON string should look as follows, meaning what you have is not JSON and hence you got JSON::ParserError for using JSON.parse on a non-JSON string :

"{\"report_name\":\"Study/Control: ABIRATERONE ACETATE - 20151413355\",\"left_mue_start_date\":\"02-26-2015\",\"left_mue_end_date\":\"03-19-2015\",\"right_mue_start_date\":\"02-26-2015\",\"right_mue_end_date\":\"03-19-2015\",\"report_formulary_id\":\",7581\",\"mue\":\"true\",\"mue_type\":\"study/control\",\"chain_id\":\"1\",\"left_mue_formulary_ids\":[\"7581\"],\"action\":\"create_report\",\"controller\":\"informatics\",\"user_id\":339}" 

JSON::ParserError Exception: 757: unexpected token at 'Token scope not set. This request does not have the required privilege.'

This sample is obsolete and not maintained, nor the Ruby package that was produced last year.
We are about to release a new package and sample to match the latest state of the API which was officially release last June. In the meantime, you need to add at least the bucket:create scope for this API as document here: https://developer.autodesk.com/en/docs/oauth/v2/overview/scopes/ and here: https://developer.autodesk.com/en/docs/data/v2/reference/http/buckets-POST/

Change the authenticate call with the appropriate scope you need. Usually data:read data:write data:update bucket:read bucket:create bucket:update

Change this line: https://github.com/GetSomeRest/adn_viewer/blob/master/lib/adn_viewer.rb#L9

JSON.parse(CurbFu.post({:host => 'developer.api.autodesk.com', :path => '/authentication/v1/authenticate', :protocol => "https"}, { :client_id => key, :client_secret => secret, :grant_type => 'client_credentials', :scope=> 'put your scopes here' }).body)

Hope that helps,

Rake task errors with: JSON::ParserError: 765: unexpected token at '' but works fine in rails console

Since you're looping over an api, it's possible there are rate limits. Public APIs normally have per second rate limits. You could try adding a sleep to slow down your requests, not sure how many your making per second. I tested with a simple loop and looks like response returns an empty string if you hit the api too fast.

uri = URI(url)
i = 1
1000.times do
puts i.to_s
i += 1
response = Net::HTTP.get(URI(uri))
j = JSON.parse(response)
puts response
#= ""

I played with this until the loop stopped returning empty string after the 3rd request and got it to work with sleep 5 inside each loop, so you can probably add as the first line inside your loop. But you should probably add error handling to your rake task in case you encounter any other API errors.

So for now you can probably just do this

until page_number > page_limit || Card.where(is_maverick: false).length == 740
sleep 5
# rest of your loop code, maybe add a rescue like I've shown

Ruby on Rails - JSON parse error unexpected token

This is not valid in json =>. JSON looks like

{ "rename": [{ "from": "TTTC" }] }

JSON::ParserError: 743: unexpected token in ROR

In your second example, r is not JSON, it is a RestClient::Response object and cannot be parsed. You need to parse the r.body of the RestClient::Response, as you referenced it with a.post.body in your second example.

r = b.post         # => <RestClient::Response 200 "{\n    \"auth...">
JSON.parse(r) # => JSON::ParserError: ...
r.body # => "Some valid JSON string"
JSON.parse(r.body) # => Parses "Some valid JSON string"

Related Topics

Leave a reply