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;
}
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
add a comment |
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
have you triedjsonlite::read_json(path = "xxx.json", simplifyVector = T)
?
– JdeMello
Nov 23 '18 at 14:08
add a comment |
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
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
r json dataframe
asked Nov 23 '18 at 12:16
penatbaterpenatbater
31
31
have you triedjsonlite::read_json(path = "xxx.json", simplifyVector = T)
?
– JdeMello
Nov 23 '18 at 14:08
add a comment |
have you triedjsonlite::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
add a comment |
1 Answer
1
active
oldest
votes
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
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
have you tried
jsonlite::read_json(path = "xxx.json", simplifyVector = T)
?– JdeMello
Nov 23 '18 at 14:08