0

I am trying to convert a json file with R in one go. Unfortunately this approach gives me an error as described below:

> fromJSON(res.clean[,2])
Error: parse error: trailing garbage
          :{"b64_value":"AAAAAA=="}}}] [{"loc":[{"N":9.03553},{"N":45.
                     (right here) ------^

if instead i do row by row, everything is ok

> fromJSON(res.clean[1,2])
                 loc  recorded_at fields.b64_value fields.b64_value fields.b64_value
1  9.03913, 45.61335 1.476451e+12         AAAC1g==             <NA>             <NA>
2  9.03924, 45.61362 1.476451e+12         AAAM+Q==             <NA>             <NA>
3  9.03995, 45.61365 1.476451e+12         AAAL2A==             <NA>             <NA>
4  9.04005, 45.61340 1.476451e+12             <NA>             <NA>             <NA>
5  9.04017, 45.61406 1.476451e+12         AAAUGg==             <NA>             <NA>
6  9.03949, 45.61419 1.476451e+12         AABLBw==             <NA>             <NA>
7  9.03496, 45.61319 1.476451e+12             <NA>         AAAABA==             <NA>
8  9.03440, 45.61295 1.476451e+12         AAArMQ==             <NA>             <NA>
9  9.03448, 45.61285 1.476451e+12             <NA>             <NA>             <NA>
10 9.03495, 45.61241 1.476451e+12         AAAAAA==             <NA>             AA==
> fromJSON(res.clean[2,2])
                 loc  recorded_at fields.b64_value fields.b64_value fields.b64_value
1  9.03553, 45.61197 1.476451e+12         AABUkQ==         AAAAAg==             <NA>
2  9.03559, 45.61188 1.476451e+12             <NA>             <NA>             <NA>
3  9.03606, 45.61129 1.476451e+12         AAAcSQ==             <NA>             <NA>
4  9.03712, 45.61127 1.476451e+12             <NA>             <NA>             <NA>
5  9.04059, 45.61095 1.476451e+12             <NA>             <NA>             <NA>
6  9.04115, 45.61091 1.476451e+12             <NA>             <NA>             <NA>
7  9.04440, 45.61064 1.476451e+12             <NA>             <NA>             <NA>
8  9.04444, 45.61067 1.476451e+12         AAAmaQ==             <NA>             <NA>
9  9.04456, 45.61115 1.476451e+12         AABq3g==             <NA>             <NA>
10 9.04445, 45.61179 1.476451e+12         AABKuQ==             <NA>             <NA>
11 9.04303, 45.61281 1.476451e+12         AABY6Q==             <NA>             <NA>
12 9.04010, 45.61327 1.476451e+12             <NA>             <NA>             <NA>
13 9.04009, 45.61331 1.476451e+12         AAAmBA==             <NA>             <NA>
14 9.03989, 45.61365 1.476452e+12             <NA>             <NA>             AA==
15 9.03989, 45.61365 1.476452e+12         AAAAAA==             <NA>             <NA>

Is there any way to do the same thing I am doing one by one all at once? I would like do avoid to run the for cycle

for (i in 1:max){
fromJSON(res.clean[i,2])}

thank you very much fro your help

Marco De Virgilis
  • 982
  • 1
  • 9
  • 29
  • try with `jsonlite::stream_in(file("yourfile.json"))`, and [see here](https://github.com/jeroen/jsonlite/issues/59) for a discussion on it – SymbolixAU Jun 12 '17 at 21:38
  • Thank you for your answer, It looks like that with stream_in you have to have either a connection or a file, what if my json is a variable in R? thanks – Marco De Virgilis Jun 13 '17 at 07:08
  • how did you create the variable in R - the JSON must enter R somehow - maybe that's where you can use the `stream_in`? – SymbolixAU Jun 13 '17 at 07:19
  • I have solved this with lapply as follows `lapply(res.clean[,2],fromJSON)` – Marco De Virgilis Jun 13 '17 at 08:14
  • fundamentally there's little difference between your `for` loop and the `lapply`. You maybe still should consider how you're getting the JSON into R in the first place. – SymbolixAU Jun 13 '17 at 08:41

0 Answers0