Reputation: 187
I have several problems when I try to extract data from JSON format file that is in a URL
When I use: jsonlite
grades=jsonlite::fromJSON("url.json") Error in open.connection(con, "rb") : HTTP error 403.
When I use RJONIO
grades=RJSONIO::fromJSON("url.json") Error in fromJSON(content, handler, default.size, depth, allowComments, : invalid JSON input
When I use rjson
grades=rjson::fromJSON("url.json") Error in rjson::fromJSON("url.json") : unexpected character 'h'
What can it be? How can I do to fix and extract the data?
Thank you
Upvotes: 3
Views: 8938
Reputation: 61
I just had a similar issue with jsonlite so I thought I would share.
Error in open.connection(con, "rb") : HTTP error 400.
Jsonlite requires a URLencode()
if there are spaces in the desired API call.
Upvotes: 5
Reputation: 7467
The rjson
package works totally fine for me. The most simple thing you can do to send a query to the API of your wishes is:
rjson::fromJSON(file = 'query to your API')
The following is a working example for a query to the OSRM routing machine, which returns a route between two points in Switzerland.
rjson::fromJSON(file = 'http://router.project-osrm.org/viaroute?loc=47,8&loc=47.3,8.5')
The result is a list that you can store or use directly. The following example gives you the travel time between these two points.
rjson::fromJSON(file = 'http://router.project-osrm.org/viarouteloc=47,8&loc=47.3,8.5')$route_summary$total_time
As long as your query is correct this should do the work.
Specific for your case it is probably better to try to read some lines first, since the file you're reading seems to be large.
line1 <- rjson::fromJSON(readLines("http://s3.amazonaws.com/databritanica/crimenesLocaciones.json", n = 1))
works perfectly for me. The parameter n
in the readLines
method specifies the number of lines to read.
Upvotes: 1