R converting FB json friend list to data frame





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a json file I'm trying to convert to a data frame. The json file looks like this, and has this pattern. (The json file is from FB, you can download your entire friendlist/profile actually in html or json format.)



   {
"friends": [
{
"name": "Archie Andrews",
"timestamp": 1539780292
},
{
"name": "Betty Cooper",
"timestamp": 1539005874
},
{
"name": "Veronica Lodge",
"timestamp": 1537680925
},
{
"name": "Sabrina Spellman",
"timestamp": 1381680968,
"contact_info": "creepyhouse@666.com"
}
]
}


In general, I'm able to convert this into a dataframe with 2 columns (name, timestamp) using this code:



  library(rjson)
friends <- fromJSON(file = "xxx.json")
data_frame <- data.frame(matrix(unlist(friends), nrow = lengths(friends)+1, byrow = T), stringsAsFactors = FALSE)


However, the annoying thing is when they have contact_info like in Sabrina's example. What happens is it gets extracted also so it skews up the arrangement. Hence the need for nrow = lengths(friends)+1



Archie Andrews      1539780292
Betty Cooper 1539005874
Veronica Lodge 1537680925
Sabrina Spellman 1381680968
creepyhouse@666.com Jughead Jones
1343582935 Midge Klump


Is there a way that when extracting the lists into 2 columns, for every list I'll just take the first 2 elements (name, timestamp)? Ultimately, I don't care for the contact_info and I just want to have a 2-column dataframe.










share|improve this question























  • have you tried jsonlite::read_json(path = "xxx.json", simplifyVector = T)?

    – JdeMello
    Nov 23 '18 at 14:08




















0















I have a json file I'm trying to convert to a data frame. The json file looks like this, and has this pattern. (The json file is from FB, you can download your entire friendlist/profile actually in html or json format.)



   {
"friends": [
{
"name": "Archie Andrews",
"timestamp": 1539780292
},
{
"name": "Betty Cooper",
"timestamp": 1539005874
},
{
"name": "Veronica Lodge",
"timestamp": 1537680925
},
{
"name": "Sabrina Spellman",
"timestamp": 1381680968,
"contact_info": "creepyhouse@666.com"
}
]
}


In general, I'm able to convert this into a dataframe with 2 columns (name, timestamp) using this code:



  library(rjson)
friends <- fromJSON(file = "xxx.json")
data_frame <- data.frame(matrix(unlist(friends), nrow = lengths(friends)+1, byrow = T), stringsAsFactors = FALSE)


However, the annoying thing is when they have contact_info like in Sabrina's example. What happens is it gets extracted also so it skews up the arrangement. Hence the need for nrow = lengths(friends)+1



Archie Andrews      1539780292
Betty Cooper 1539005874
Veronica Lodge 1537680925
Sabrina Spellman 1381680968
creepyhouse@666.com Jughead Jones
1343582935 Midge Klump


Is there a way that when extracting the lists into 2 columns, for every list I'll just take the first 2 elements (name, timestamp)? Ultimately, I don't care for the contact_info and I just want to have a 2-column dataframe.










share|improve this question























  • have you tried jsonlite::read_json(path = "xxx.json", simplifyVector = T)?

    – JdeMello
    Nov 23 '18 at 14:08
















0












0








0








I have a json file I'm trying to convert to a data frame. The json file looks like this, and has this pattern. (The json file is from FB, you can download your entire friendlist/profile actually in html or json format.)



   {
"friends": [
{
"name": "Archie Andrews",
"timestamp": 1539780292
},
{
"name": "Betty Cooper",
"timestamp": 1539005874
},
{
"name": "Veronica Lodge",
"timestamp": 1537680925
},
{
"name": "Sabrina Spellman",
"timestamp": 1381680968,
"contact_info": "creepyhouse@666.com"
}
]
}


In general, I'm able to convert this into a dataframe with 2 columns (name, timestamp) using this code:



  library(rjson)
friends <- fromJSON(file = "xxx.json")
data_frame <- data.frame(matrix(unlist(friends), nrow = lengths(friends)+1, byrow = T), stringsAsFactors = FALSE)


However, the annoying thing is when they have contact_info like in Sabrina's example. What happens is it gets extracted also so it skews up the arrangement. Hence the need for nrow = lengths(friends)+1



Archie Andrews      1539780292
Betty Cooper 1539005874
Veronica Lodge 1537680925
Sabrina Spellman 1381680968
creepyhouse@666.com Jughead Jones
1343582935 Midge Klump


Is there a way that when extracting the lists into 2 columns, for every list I'll just take the first 2 elements (name, timestamp)? Ultimately, I don't care for the contact_info and I just want to have a 2-column dataframe.










share|improve this question














I have a json file I'm trying to convert to a data frame. The json file looks like this, and has this pattern. (The json file is from FB, you can download your entire friendlist/profile actually in html or json format.)



   {
"friends": [
{
"name": "Archie Andrews",
"timestamp": 1539780292
},
{
"name": "Betty Cooper",
"timestamp": 1539005874
},
{
"name": "Veronica Lodge",
"timestamp": 1537680925
},
{
"name": "Sabrina Spellman",
"timestamp": 1381680968,
"contact_info": "creepyhouse@666.com"
}
]
}


In general, I'm able to convert this into a dataframe with 2 columns (name, timestamp) using this code:



  library(rjson)
friends <- fromJSON(file = "xxx.json")
data_frame <- data.frame(matrix(unlist(friends), nrow = lengths(friends)+1, byrow = T), stringsAsFactors = FALSE)


However, the annoying thing is when they have contact_info like in Sabrina's example. What happens is it gets extracted also so it skews up the arrangement. Hence the need for nrow = lengths(friends)+1



Archie Andrews      1539780292
Betty Cooper 1539005874
Veronica Lodge 1537680925
Sabrina Spellman 1381680968
creepyhouse@666.com Jughead Jones
1343582935 Midge Klump


Is there a way that when extracting the lists into 2 columns, for every list I'll just take the first 2 elements (name, timestamp)? Ultimately, I don't care for the contact_info and I just want to have a 2-column dataframe.







r json dataframe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 12:16









penatbaterpenatbater

31




31













  • have you tried jsonlite::read_json(path = "xxx.json", simplifyVector = T)?

    – JdeMello
    Nov 23 '18 at 14:08





















  • have you tried jsonlite::read_json(path = "xxx.json", simplifyVector = T)?

    – JdeMello
    Nov 23 '18 at 14:08



















have you tried jsonlite::read_json(path = "xxx.json", simplifyVector = T)?

– JdeMello
Nov 23 '18 at 14:08







have you tried jsonlite::read_json(path = "xxx.json", simplifyVector = T)?

– JdeMello
Nov 23 '18 at 14:08














1 Answer
1






active

oldest

votes


















0














If I understand your question correctly, you can remove the columns afterwards. Note that jsonlite::read_json or jsonlite::fromJSON converts the xxx.json file into a list object in which the first element of this list is a data.frame. You can extract elements from this list by using the [[ subsetting operator.



df <- jsonlite::read_json(path = "test.json", simplifyDataFrame = T)[[1]] ## note the "[[" subseting operator

df <- df[, c("name", "timestamp")] ## select the columns as desired


Result:



> df
name timestamp
1 Archie Andrews 1539780292
2 Betty Cooper 1539005874
3 Veronica Lodge 1537680925
4 Sabrina Spellman 1381680968





share|improve this answer
























  • Oh oh oh! This works! Thanks! I didn't know about the read_json function. Learn something new everyday!

    – penatbater
    Nov 23 '18 at 14:34












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446579%2fr-converting-fb-json-friend-list-to-data-frame%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














If I understand your question correctly, you can remove the columns afterwards. Note that jsonlite::read_json or jsonlite::fromJSON converts the xxx.json file into a list object in which the first element of this list is a data.frame. You can extract elements from this list by using the [[ subsetting operator.



df <- jsonlite::read_json(path = "test.json", simplifyDataFrame = T)[[1]] ## note the "[[" subseting operator

df <- df[, c("name", "timestamp")] ## select the columns as desired


Result:



> df
name timestamp
1 Archie Andrews 1539780292
2 Betty Cooper 1539005874
3 Veronica Lodge 1537680925
4 Sabrina Spellman 1381680968





share|improve this answer
























  • Oh oh oh! This works! Thanks! I didn't know about the read_json function. Learn something new everyday!

    – penatbater
    Nov 23 '18 at 14:34
















0














If I understand your question correctly, you can remove the columns afterwards. Note that jsonlite::read_json or jsonlite::fromJSON converts the xxx.json file into a list object in which the first element of this list is a data.frame. You can extract elements from this list by using the [[ subsetting operator.



df <- jsonlite::read_json(path = "test.json", simplifyDataFrame = T)[[1]] ## note the "[[" subseting operator

df <- df[, c("name", "timestamp")] ## select the columns as desired


Result:



> df
name timestamp
1 Archie Andrews 1539780292
2 Betty Cooper 1539005874
3 Veronica Lodge 1537680925
4 Sabrina Spellman 1381680968





share|improve this answer
























  • Oh oh oh! This works! Thanks! I didn't know about the read_json function. Learn something new everyday!

    – penatbater
    Nov 23 '18 at 14:34














0












0








0







If I understand your question correctly, you can remove the columns afterwards. Note that jsonlite::read_json or jsonlite::fromJSON converts the xxx.json file into a list object in which the first element of this list is a data.frame. You can extract elements from this list by using the [[ subsetting operator.



df <- jsonlite::read_json(path = "test.json", simplifyDataFrame = T)[[1]] ## note the "[[" subseting operator

df <- df[, c("name", "timestamp")] ## select the columns as desired


Result:



> df
name timestamp
1 Archie Andrews 1539780292
2 Betty Cooper 1539005874
3 Veronica Lodge 1537680925
4 Sabrina Spellman 1381680968





share|improve this answer













If I understand your question correctly, you can remove the columns afterwards. Note that jsonlite::read_json or jsonlite::fromJSON converts the xxx.json file into a list object in which the first element of this list is a data.frame. You can extract elements from this list by using the [[ subsetting operator.



df <- jsonlite::read_json(path = "test.json", simplifyDataFrame = T)[[1]] ## note the "[[" subseting operator

df <- df[, c("name", "timestamp")] ## select the columns as desired


Result:



> df
name timestamp
1 Archie Andrews 1539780292
2 Betty Cooper 1539005874
3 Veronica Lodge 1537680925
4 Sabrina Spellman 1381680968






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 14:25









JdeMelloJdeMello

781418




781418













  • Oh oh oh! This works! Thanks! I didn't know about the read_json function. Learn something new everyday!

    – penatbater
    Nov 23 '18 at 14:34



















  • Oh oh oh! This works! Thanks! I didn't know about the read_json function. Learn something new everyday!

    – penatbater
    Nov 23 '18 at 14:34

















Oh oh oh! This works! Thanks! I didn't know about the read_json function. Learn something new everyday!

– penatbater
Nov 23 '18 at 14:34





Oh oh oh! This works! Thanks! I didn't know about the read_json function. Learn something new everyday!

– penatbater
Nov 23 '18 at 14:34




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446579%2fr-converting-fb-json-friend-list-to-data-frame%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy