2

I am doing this

newzips=fromJSON("http://media.mongodb.org/zips.json")

You can see the data yourself at http://media.mongodb.org/zips.json

and getting thus

str(newzips)
 List of 5
 $ city : chr "ACMAR"
 $ loc  : num [1:2] -86.5 33.6
 $ pop  : num 6055
 $ state: chr "AL"
 $ _id  : chr "35004\"}{\"city\":\"ADAMSVILLE\",\"loc\":[-86.959727,33.588437],\"pop\":10616,\"state\":\"AL\",\"_
Henrik
  • 65,555
  • 14
  • 143
  • 159
Ajay Ohri
  • 3,382
  • 3
  • 30
  • 60
  • Think you have to read it and convert line by line, or join it with commas and convert it... – Spacedman Jul 01 '14 at 16:00
  • 3
    Don't really understand the downvotes here - question is reproducible, output is given etc. Reasonable to expect a .json file is truly JSON... – Spacedman Jul 01 '14 at 16:15

2 Answers2

7

This format is called jsonlines. You can import it using the stream_in function in jsonite:

library(jsonlite)
zips <- stream_in(url("http://media.mongodb.org/zips.json"))

If the server uses https, you can use the curl package:

library(jsonlite)
library(curl)
zips <- stream_in(curl("https://media.mongodb.org/zips.json"))

Datasets where each line is a record are usually nosql database dumps. Because they might be too large to parse all at once, they are meant to be imported line-by-line, which is exactly what jsonlite does.

Jeroen Ooms
  • 31,998
  • 35
  • 134
  • 207
4

readLines + mush it into a JS array with some brackets and comma-separation:

> json = fromJSON(paste("[",paste(readLines("http://media.mongodb.org/zips.json"),collapse=","),"]"))
Warning message:
In readLines("http://media.mongodb.org/zips.json") :
  incomplete final line found on 'http://media.mongodb.org/zips.json'
> head(json)
        city                 loc   pop state   _id
1      ACMAR -86.51557, 33.58413  6055    AL 35004
2 ADAMSVILLE -86.95973, 33.58844 10616    AL 35005
3      ADGER -87.16746, 33.43428  3205    AL 35006
4   KEYSTONE -86.81286, 33.23687 14218    AL 35007
5   NEW SITE -85.95109, 32.94145 19942    AL 35010
6     ALPINE -86.20893, 33.33116  3062    AL 35014
Spacedman
  • 92,590
  • 12
  • 140
  • 224