Parse Error: "Trailing Garbage" while trying to parse JSON column in data frame
Asked Answered
T

1

23

I have a log file that look like this. This is a text document that looks like:

Id,Date,Level,Message
35054,2016-06-17 19:29:43 +0000,INFO,"{
  ""id"": -2,
  ""ipAddress"": ""100.100.100.100"",
  ""howYouHearAboutUs"": null,
  ""isInterestedInOffer"": true,
  ""incomeRange"": 60000,
  ""isEmailConfirmed"": false
}"
35055,2016-06-17 19:36:38 +0000,INFO,"{
  ""id"": -1,
  ""firstName"": ""John"",
  ""lastName"": ""Smith"",
  ""email"": ""[email protected]"",
  ""city"": ""Smalltown"",
  ""incomeRange"": 1,
  ""birthDate"": ""1999-12-10T05:00:00Z"",
  ""password"": ""*********"",
  ""agreeToTermsOfUse"": true,
  ""howYouHearAboutUs"": ""Radio"",
  ""isInterestedInOffer"": false
}"
35059,2016-07-19 19:52:08 +0000,INFO,"{
  ""id"": -3,
  ""visitUrl"": ""https://www.website.com/?purpose=X"",
  ""ipAddress"": ""100.200.300.400"",
  ""howYouHearAboutUs"": null,
  ""isInterestedInOffer"": true,
  ""incomeRange"": 100000,
  ""isEmailConfirmed"": true,
  ""isIdentityConfirmed"": false,
  ""agreeToTermsOfUse"": true,
  ""validationResults"": null
}"

I'm trying to parse the JSON in the Message column by:

library(readr)
library(jsonlite)

df <- read_csv("log_file_from_above.csv")
fromJSON(as.character(df$Message))

But, I'm hitting the following error:

Error: parse error: trailing garbage
          "isEmailConfirmed": false  } {    "id": -1,    "firstName": 
                     (right here) ------^

How can I get rid of the "trailing garbage"?

Tamaratamarack answered 9/8, 2016 at 18:52 Comment(2)
lapply(paste0("[",df$Message,"]"), function(x) jsonlite::fromJSON(x)) yields something?Maravedi
I copied a block of json data from an html document into a new text file and was also getting this error. Echoing the above comment, what solved this problem for me was manually adding a single open bracket ( [ ) at the top of my json data text file and a single close bracket ( ] ) at the end.Beaux
H
25

fromJSON() isn't "apply"ing against the character vector, it's trying to convert it all to a data frame. You can try

purrr::map(df$Message, jsonlite::fromJSON)

what @Abdou provided or

jsonlite::stream_in(textConnection(gsub("\\n", "", df$Message)))

The latter two will create data frames. The first will create a list you can add as a column.

You can use the last method with dplyr::bind_cols to make a new data frame with all the data:

dplyr::bind_cols(df[,1:3],
                 jsonlite::stream_in(textConnection(gsub("\\n", "", df$Message))))

Also suggested by @Abdou is an almost pure base R solution:

cbind(df, do.call(plyr::rbind.fill, lapply(paste0("[",df$Message,"]"), function(x) jsonlite::fromJSON(x))))

Full, working, workflow:

library(dplyr)
library(jsonlite)

df <- read.table("http://pastebin.com/raw/MMPMwNZv",
                 quote='"', sep=",", stringsAsFactors=FALSE, header=TRUE)

bind_cols(df[,1:3], stream_in(textConnection(gsub("\\n", "", df$Message)))) %>%
  glimpse()
## 
 Found 3 records...
 Imported 3 records. Simplifying into dataframe...
## Observations: 3
## Variables: 19
## $ Id                  <int> 35054, 35055, 35059
## $ Date                <chr> "2016-06-17 19:29:43 +0000", "2016-06-17 1...
## $ Level               <chr> "INFO", "INFO", "INFO"
## $ id                  <int> -2, -1, -3
## $ ipAddress           <chr> "100.100.100.100", NA, "100.200.300.400"
## $ howYouHearAboutUs   <chr> NA, "Radio", NA
## $ isInterestedInOffer <lgl> TRUE, FALSE, TRUE
## $ incomeRange         <int> 60000, 1, 100000
## $ isEmailConfirmed    <lgl> FALSE, NA, TRUE
## $ firstName           <chr> NA, "John", NA
## $ lastName            <chr> NA, "Smith", NA
## $ email               <chr> NA, "[email protected]", NA
## $ city                <chr> NA, "Smalltown", NA
## $ birthDate           <chr> NA, "1999-12-10T05:00:00Z", NA
## $ password            <chr> NA, "*********", NA
## $ agreeToTermsOfUse   <lgl> NA, TRUE, TRUE
## $ visitUrl            <chr> NA, NA, "https://www.website.com/?purpose=X"
## $ isIdentityConfirmed <lgl> NA, NA, FALSE
## $ validationResults   <lgl> NA, NA, NA
Hutchins answered 9/8, 2016 at 19:13 Comment(3)
How can I use purrr::map(df$Message, jsonlite::fromJSON) in a dataframe? So that I don't lose the time stamps?Tamaratamarack
@hrbrmstr, can you add cbind(df[,1:3], do.call(plyr::rbind.fill, lapply(paste0("[",df$Message,"]"), function(x) jsonlite::fromJSON(x))))? It uses the plyr package.Maravedi
Still getting a parse error with dplyr::bind_cols(df[,1:3], jsonlite::stream_in(textConnection(gsub("\\n", "", df$Message)))) ? Are you using the pastebin file with the weird indentation kept intact?Tamaratamarack

© 2022 - 2024 — McMap. All rights reserved.