Duck
Duck

Reputation: 39595

Format JSON object as dataframe in R

I am working with a JSON object in R. Actually my data looks like this:

#Data
v1 <- "{\"data\":{\"id\":25945306,\"full_name\":\"badgalriri\",\"username\":\"badgalriri\",\"profile_picture\":{\"normal\":\"https:\\/\\/scontent-frt3-1.cdninstagram.com\\/v\\/t51.2885-19\\/11032926_1049846535031474_260957621_a.jpg?_nc_ht=scontent-frt3-1.cdninstagram.com&_nc_ohc=Z3JOqyO0wu4AX8eB_8u&edm=ABfd0MgBAAAA&ccb=7-4&oh=1e3372e82cfecc3209fa9c16f90073f2&oe=615BCFBA&_nc_sid=7bff83\",\"hd\":\"https:\\/\\/scontent-frt3-1.cdninstagram.com\\/v\\/t51.2885-19\\/11032926_1049846535031474_260957621_a.jpg?_nc_ht=scontent-frt3-1.cdninstagram.com&_nc_ohc=Z3JOqyO0wu4AX8eB_8u&edm=ABfd0MgBAAAA&ccb=7-4&oh=1e3372e82cfecc3209fa9c16f90073f2&oe=615BCFBA&_nc_sid=7bff83\"},\"biography\":\"\",\"website\":\"http:\\/\\/ri-hanna.io\\/savagex\",\"figures\":{\"posts\":4787,\"followers\":109158907,\"followings\":1563},\"is_private\":false,\"is_verified\":true,\"is_business\":false,\"is_joined_recently\":false,\"has_channel\":false,\"reel\":{\"highlight\":{\"count\":17}},\"business\":{\"category\":null,\"phone\":null,\"email\":null,\"address\":null},\"facebook_page\":null},\"message\":\"OK\",\"code\":200}"

This is a very complex string to format and the only thing I could reach was this:

library(jsonlite)
#Code
v2 <- fromJSON(v1)

Where I get a list pretty formatted with three slots. I have tried to transform as dataframe the data slot but I got a error:

as.data.frame(v2$data)
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 1, 0

Maybe due the fact that some slots have multiple elements:

v2$data$business
$category
NULL

$phone
NULL

$email
NULL

$address
NULL

How can I get all the elements from v2$Data formatted as dataframe, even including all elements in sublists (as those in previous list showed) as columns in the final dataframe.

Many thanks!

Upvotes: 2

Views: 52

Answers (1)

akrun
akrun

Reputation: 886948

It is a nested list, we may need to flatten it

library(rrapply)
rrapply(fromJSON(v1)$data, how = 'bind')

If we need to convert the NULL to NA

rrapply(fromJSON(v1)$data, f = function(x) replace(x, is.null(x), NA), how = 'bind')
        id  full_name   username
1 25945306 badgalriri badgalriri
                                                                                                                                                                                                                                        profile_picture.normal
1 https://scontent-frt3-1.cdninstagram.com/v/t51.2885-19/11032926_1049846535031474_260957621_a.jpg?_nc_ht=scontent-frt3-1.cdninstagram.com&_nc_ohc=Z3JOqyO0wu4AX8eB_8u&edm=ABfd0MgBAAAA&ccb=7-4&oh=1e3372e82cfecc3209fa9c16f90073f2&oe=615BCFBA&_nc_sid=7bff83
                                                                                                                                                                                                                                            profile_picture.hd
1 https://scontent-frt3-1.cdninstagram.com/v/t51.2885-19/11032926_1049846535031474_260957621_a.jpg?_nc_ht=scontent-frt3-1.cdninstagram.com&_nc_ohc=Z3JOqyO0wu4AX8eB_8u&edm=ABfd0MgBAAAA&ccb=7-4&oh=1e3372e82cfecc3209fa9c16f90073f2&oe=615BCFBA&_nc_sid=7bff83
  biography                    website figures.posts figures.followers figures.followings is_private is_verified is_business is_joined_recently
1           http://ri-hanna.io/savagex          4787         109158907               1563      FALSE        TRUE       FALSE              FALSE
  has_channel reel.highlight.count business.category business.phone business.email business.address facebook_page
1       FALSE                   17                NA             NA             NA               NA            NA

Upvotes: 2

Related Questions