1

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"}}
aks2200
  • 73
  • 5
  • *What* error? How did you try to convert this? – Panagiotis Kanavos Feb 08 '17 at 17:04
  • 3
    PS - you can't expect people to download a 130MB file. Post a small example of the contents in the question itself – Panagiotis Kanavos Feb 08 '17 at 17:05
  • Perhaps helpful ... please read about [minimal](http://stackoverflow.com/help/mcve) and [reproducible](http://stackoverflow.com/a/5963610/3358272) questions. Welcome to SO! Questions have a much higher likelihood of being answered (and faster even) when you read and heed those two short guides. Otherwise your question is bound to be ignored, closed, or answered incorrectly, none of which help you. – r2evans Feb 08 '17 at 17:08
  • Just updated my question again with little bit more details. – aks2200 Feb 08 '17 at 17:10
  • You snippet is not a valid JSON as items must be comma-separated within `[]` or `{}` containers. Please include proper few rows of data so we can reproduce. – Parfait Feb 08 '17 at 18:50

2 Answers2

0

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

nate
  • 1,172
  • 1
  • 11
  • 26
0

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)))))
aks2200
  • 73
  • 5