简体   繁体   中英

Converting JSON file to data.frame in R

I have a file which contain event data(click data of users interaction with app) in JSON format, I need to convert it in data frame. I used this:

require(RJSONIO)
result <- fromJSON('events_data.json',nullValue = NA)
result <- do.call(rbind,lapply(result,data.frame,stringsAsFactors=FALSE))

which generates this result

summary(result)
                  Length Class  Mode     
_id               1      -none- character
session           2      -none- list     
metrics           0      -none- list     
arrival_timestamp 1      -none- character
event_type        1      -none- character
event_timestamp   1      -none- character
event_version     1      -none- character
application       7      -none- list     
client            2      -none- character
device            4      -none- list     
attributes        3      -none- character

When I am trying to convert this list of list into data frame I get an error

Error in data.frame(locale = c("US", "en_US", "en"), platform = c("5.1.1",  : 
  arguments imply differing number of rows: 3, 2, 1

Here is the data file, click here (Just for reference). Can someone help me out. JSON file contain

{"_id":{"$oid":"57a30ce268fd0809ec4d194f"},"session":{"start_timestamp":{"$numberLong":"1470183490481"},"session_id":"def5faa9-20160803-001810481"},"metrics":{},"arrival_timestamp":{"$numberLong":"1470183523054"},"event_type":"OfferViewed","event_timestamp":{"$numberLong":"1470183505399"},"event_version":"3.0","application":{"package_name":"com.think.vito","title":"Vito","version_code":"5","app_id":"7ffa58dab3c646cea642e961ff8a8070","cognito_identity_pool_id":"us-east-1:4d9cf803-0487-44ec-be27-1e160d15df74","version_name":"2.0.0.0","sdk":{"version":"2.2.2","name":"aws-sdk-android"}},"client":{"cognito_id":"us-east-1:2e26918b-f7b1-471e-9df4-b931509f7d37","client_id":"ee0b61b0-85cf-4b2f-960e-e2aedef5faa9"},"device":{"locale":{"country":"US","code":"en_US","language":"en"},"platform":{"version":"5.1.1","name":"ANDROID"},"make":"YU","model":"AO5510"},"attributes":{"Category":"120000","CustomerID":"4078","OfferID":"45436"}}
{"_id":{"$oid":"57a30ce268fd0809ec4d1950"},"session":{"start_timestamp":{"$numberLong":"1470183490481"},"session_id":"def5faa9-20160803-001810481"},"metrics":{},"arrival_timestamp":{"$numberLong":"1470183523054"},"event_type":"ContextMenuItemSelected","event_timestamp":{"$numberLong":"1470183500206"},"event_version":"3.0","application":{"package_name":"com.think.vito","title":"Vito","version_code":"5","app_id":"7ffa58dab3c646cea642e961ff8a8070","cognito_identity_pool_id":"us-east-1:4d9cf803-0487-44ec-be27-1e160d15df74","version_name":"2.0.0.0","sdk":{"version":"2.2.2","name":"aws-sdk-android"}},"client":{"cognito_id":"us-east-1:2e26918b-f7b1-471e-9df4-b931509f7d37","client_id":"ee0b61b0-85cf-4b2f-960e-e2aedef5faa9"},"device":{"locale":{"country":"US","code":"en_US","language":"en"},"platform":{"version":"5.1.1","name":"ANDROID"},"make":"YU","model":"AO5510"},"attributes":{"MenuItem":"OfferList","CustomerID":"4078"}}

I was able to download and read in your data just fine. Use the ndjson library. Check this out. Let me know if it gets you what you're looking for...

df<-ndjson::stream_in('events_data.json')

Good luck. Cheers, NF

I got my answer, actually I wasn't reading whole json file.

library(rjson)
library(plyr)

json_file <- "events_data.json"

con <- file(json_file, "r")
input <- readLines(con,-1L)
close(con)

json_file2 <- ldply(lapply(input, function(x) t(unlist(fromJSON(x)))))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM