Parsing nested JSON in a Data Frame in R

I have problems with a very nasty nested JSON.

The format is as follows

{ "matches": [ { "matchId": 1, "region": "BR", "participants": [ { "participantId": 0, "teamId": 200, "stats": { "winner": true, "champLevel": 16, "item0": 3128, } { "matchId": 2, "region": "BR", "participants": [ { "participantId": 0, "teamId": 201, "stats": { "winner": false, "champLevel": 18, "item0": 3128, "item1": 3157, "item1": 3158, } 

As you can see in the second match, the number of elements increased, but in the data frame, the first row will have the same columns:

 MatchId region ... stats.winner stats.champLevel stats.item0 stats.item1 stats.item2 1 BR TRUE 16 3128 1 BR 1 BR TRUE 16 3128 3157 3158 

See the first line is less than the second, so R processes the values ​​....

If you want to get the full data, you can capture it: http://pastebin.com/HQDf2ase

How I parsed json for data.frame:

 json.matchData <- fromJSON(file="file.json")) 

Discard Json elements and convert them to data frame

 matchData.i <- lapply(json.matchData$matches, function(x){ unlist(x)}) 

Convert to data frame

 matchData <- do.call("rbind", matchData.i) matchData <- as.data.frame(matchData) 

But the data frame is corrupted, because some fields must be NA, but they are filled with incorrect values.

+5
source share
1 answer

I think using the plyr rbind.fill() function would be useful here. How about this

 library(plyr) matchData <- rbind.fill(lapply(matchData.i, function(x) do.call("data.frame", as.list(x)) )) 

the lapply() bit should turn the intermediate lists into data.frames, which rbind.fill requires.

+4
source

Source: https://habr.com/ru/post/1208837/


All Articles