2

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!

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

Many thanks, maybe is there a way to use any tidyverse package for that! I will upvote!
@StackGuy For recursive list elements, rrapply or rapply from base R can be used. There is no function in tidyverse that does recursive
I see, I will upvote. Many thanks for your help!
@StackGuy what is your expected output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.