2
votes

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

2
rjson::fromJSON("query to the API I'm trying to reach") works totally fine for me. What's the URL you're trying to read from?symbolrush
is a json file from s3.amazonaws.comRivero Felipe
Can you place the query you're sending there? Because "url.json" won't give back any results..symbolrush

2 Answers

4
votes

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.

1
votes

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 nin the readLines method specifies the number of lines to read.