parse json with embedded lists into semi-long dataframe
up vote
0
down vote
favorite
I have a json file with several layers of nesting, and I am struggling to get it into a workable dataframe. I created a toy example of mock data based on a real structure: here is the gist.
And here is my desired output. The output could be "longer" or have additional variables from the original json, but I'm showing the core ask.
This is the part of the json that shows the deepest level of nesting that I want to get into a semi-long format as shown above in white (a fully wide format would be fine).
I've tried lots of things with this object:
myList <- jsonlite::fromJSON("example.json", flatten=TRUE)$results
from trying to subset []
and cbind()
, to other efforts trying to unnest the embedded lists. Nothing quite right. I'd benefit greatly from advice on the best approach.
r json jsonlite
add a comment |
up vote
0
down vote
favorite
I have a json file with several layers of nesting, and I am struggling to get it into a workable dataframe. I created a toy example of mock data based on a real structure: here is the gist.
And here is my desired output. The output could be "longer" or have additional variables from the original json, but I'm showing the core ask.
This is the part of the json that shows the deepest level of nesting that I want to get into a semi-long format as shown above in white (a fully wide format would be fine).
I've tried lots of things with this object:
myList <- jsonlite::fromJSON("example.json", flatten=TRUE)$results
from trying to subset []
and cbind()
, to other efforts trying to unnest the embedded lists. Nothing quite right. I'd benefit greatly from advice on the best approach.
r json jsonlite
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a json file with several layers of nesting, and I am struggling to get it into a workable dataframe. I created a toy example of mock data based on a real structure: here is the gist.
And here is my desired output. The output could be "longer" or have additional variables from the original json, but I'm showing the core ask.
This is the part of the json that shows the deepest level of nesting that I want to get into a semi-long format as shown above in white (a fully wide format would be fine).
I've tried lots of things with this object:
myList <- jsonlite::fromJSON("example.json", flatten=TRUE)$results
from trying to subset []
and cbind()
, to other efforts trying to unnest the embedded lists. Nothing quite right. I'd benefit greatly from advice on the best approach.
r json jsonlite
I have a json file with several layers of nesting, and I am struggling to get it into a workable dataframe. I created a toy example of mock data based on a real structure: here is the gist.
And here is my desired output. The output could be "longer" or have additional variables from the original json, but I'm showing the core ask.
This is the part of the json that shows the deepest level of nesting that I want to get into a semi-long format as shown above in white (a fully wide format would be fine).
I've tried lots of things with this object:
myList <- jsonlite::fromJSON("example.json", flatten=TRUE)$results
from trying to subset []
and cbind()
, to other efforts trying to unnest the embedded lists. Nothing quite right. I'd benefit greatly from advice on the best approach.
r json jsonlite
r json jsonlite
asked Nov 9 at 19:12
Eric Green
2,41753163
2,41753163
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Does this get you any further along? (This is a gnarly structure):
library(tidyverse)
x <- (jsonlite::fromJSON("/Users/hrbrmstr/r7/gh/labs-research/2018-11-portland-ciso-event/example.json"))
jsonlite::stream_out(x$results, con = gzfile("ex-res.json.gz"))
y <- ndjson::stream_in("ex-res.json.gz", "tbl")
gather(y, path, path_val, starts_with("path")) %>%
gather(flow, flow_val, starts_with("flow")) %>%
gather(name, name_val, starts_with("values.pdep")) %>%
gather(intervention, interv_val, starts_with("values.inter")) %>%
glimpse()
## Observations: 87,696
## Variables: 18
## $ contact.name <chr> "Person 1", "Person 2", "Person 1", "Person 2", "Person 1", "Person 2", "Person 1", "Person 2"...
## $ contact.uuid <chr> "k0dcjs", "rd3jfui", "k0dcjs", "rd3jfui", "k0dcjs", "rd3jfui", "k0dcjs", "rd3jfui", "k0dcjs", ...
## $ created_on <chr> "2016-02-08T07:00:15.093813Z", "2016-02-08T07:00:15.093813Z", "2016-02-08T07:00:15.093813Z", "...
## $ id <dbl> 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235...
## $ modified_on <chr> "2016-02-09T04:42:54.812323Z", "2016-02-08T08:09:51.545160Z", "2016-02-09T04:42:54.812323Z", "...
## $ responded <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE...
## $ start.uuid <chr> "dnxh4g", "kfj4dsi", "dnxh4g", "kfj4dsi", "dnxh4g", "kfj4dsi", "dnxh4g", "kfj4dsi", "dnxh4g", ...
## $ uuid <chr> "esn4dk", "qask9dj", "esn4dk", "qask9dj", "esn4dk", "qask9dj", "esn4dk", "qask9dj", "esn4dk", ...
## $ exit_type <chr> NA, "completed", NA, "completed", NA, "completed", NA, "completed", NA, "completed", NA, "comp...
## $ exited_on <chr> NA, "2016-02-08T08:09:51.544998Z", NA, "2016-02-08T08:09:51.544998Z", NA, "2016-02-08T08:09:51...
## $ path <chr> "path.0.node", "path.0.node", "path.0.time", "path.0.time", "path.1.node", "path.1.node", "pat...
## $ path_val <chr> "ecb4cb11-6cca-4791-a950-c448e9300846", "ecb4cb11-6cca-4791-a950-c448e9300846", "2016-02-08T07...
## $ flow <chr> "flow.name", "flow.name", "flow.name", "flow.name", "flow.name", "flow.name", "flow.name", "fl...
## $ flow_val <chr> "weeklyratings", "weeklyratings", "weeklyratings", "weeklyratings", "weeklyratings", "weeklyra...
## $ name <chr> "values.pdeps1.category", "values.pdeps1.category", "values.pdeps1.category", "values.pdeps1.c...
## $ name_val <chr> "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 -...
## $ intervention <chr> "values.intervention", "values.intervention", "values.intervention", "values.intervention", "v...
## $ interv_val <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA...
Full approach:
gather(y, path, path_val, starts_with("path")) %>%
gather(flow, flow_val, starts_with("flow")) %>%
gather(name, name_val, starts_with("values.pdep")) %>%
gather(intervention, interv_val, starts_with("values.inter")) %>%
filter(grepl(".value", name)) %>%
filter(grepl("node", path)) %>%
mutate(variable = gsub("values.", "", name)) %>%
mutate(variable = gsub(".value", "", variable)) %>%
distinct(contact.name, uuid, name, .keep_all = TRUE) %>%
select(id, uuid, contact.uuid, variable, name_val, created_on, modified_on) %>%
arrange(id, created_on) # optional wide %>% spread(variable, name_val)
Awesome, @hrbmstr! I was able to take this and get it the rest of the way. Shall I make an edit to your answer to suggest the method?
– Eric Green
Nov 9 at 20:01
messy as a comment, but replaceglimpse()
with:filter(grepl(".value", name)) %>% filter(grepl("node", path)) %>% mutate(variable = gsub("values.", "", name)) %>% mutate(variable = gsub(".value", "", variable)) %>% distinct(contact.name, uuid, name, .keep_all = TRUE) %>% select(id, uuid, contact.uuid, variable, name_val, created_on, modified_on) %>% arrange(id, created_on) # optional wide %>% spread(variable, name_val)
– Eric Green
Nov 9 at 20:30
1
definitely go ahead and edit it. just glad it helped. i've had my share of gnarly json in the past.
– hrbrmstr
Nov 9 at 20:37
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',
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%2f53231968%2fparse-json-with-embedded-lists-into-semi-long-dataframe%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
up vote
2
down vote
accepted
Does this get you any further along? (This is a gnarly structure):
library(tidyverse)
x <- (jsonlite::fromJSON("/Users/hrbrmstr/r7/gh/labs-research/2018-11-portland-ciso-event/example.json"))
jsonlite::stream_out(x$results, con = gzfile("ex-res.json.gz"))
y <- ndjson::stream_in("ex-res.json.gz", "tbl")
gather(y, path, path_val, starts_with("path")) %>%
gather(flow, flow_val, starts_with("flow")) %>%
gather(name, name_val, starts_with("values.pdep")) %>%
gather(intervention, interv_val, starts_with("values.inter")) %>%
glimpse()
## Observations: 87,696
## Variables: 18
## $ contact.name <chr> "Person 1", "Person 2", "Person 1", "Person 2", "Person 1", "Person 2", "Person 1", "Person 2"...
## $ contact.uuid <chr> "k0dcjs", "rd3jfui", "k0dcjs", "rd3jfui", "k0dcjs", "rd3jfui", "k0dcjs", "rd3jfui", "k0dcjs", ...
## $ created_on <chr> "2016-02-08T07:00:15.093813Z", "2016-02-08T07:00:15.093813Z", "2016-02-08T07:00:15.093813Z", "...
## $ id <dbl> 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235...
## $ modified_on <chr> "2016-02-09T04:42:54.812323Z", "2016-02-08T08:09:51.545160Z", "2016-02-09T04:42:54.812323Z", "...
## $ responded <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE...
## $ start.uuid <chr> "dnxh4g", "kfj4dsi", "dnxh4g", "kfj4dsi", "dnxh4g", "kfj4dsi", "dnxh4g", "kfj4dsi", "dnxh4g", ...
## $ uuid <chr> "esn4dk", "qask9dj", "esn4dk", "qask9dj", "esn4dk", "qask9dj", "esn4dk", "qask9dj", "esn4dk", ...
## $ exit_type <chr> NA, "completed", NA, "completed", NA, "completed", NA, "completed", NA, "completed", NA, "comp...
## $ exited_on <chr> NA, "2016-02-08T08:09:51.544998Z", NA, "2016-02-08T08:09:51.544998Z", NA, "2016-02-08T08:09:51...
## $ path <chr> "path.0.node", "path.0.node", "path.0.time", "path.0.time", "path.1.node", "path.1.node", "pat...
## $ path_val <chr> "ecb4cb11-6cca-4791-a950-c448e9300846", "ecb4cb11-6cca-4791-a950-c448e9300846", "2016-02-08T07...
## $ flow <chr> "flow.name", "flow.name", "flow.name", "flow.name", "flow.name", "flow.name", "flow.name", "fl...
## $ flow_val <chr> "weeklyratings", "weeklyratings", "weeklyratings", "weeklyratings", "weeklyratings", "weeklyra...
## $ name <chr> "values.pdeps1.category", "values.pdeps1.category", "values.pdeps1.category", "values.pdeps1.c...
## $ name_val <chr> "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 -...
## $ intervention <chr> "values.intervention", "values.intervention", "values.intervention", "values.intervention", "v...
## $ interv_val <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA...
Full approach:
gather(y, path, path_val, starts_with("path")) %>%
gather(flow, flow_val, starts_with("flow")) %>%
gather(name, name_val, starts_with("values.pdep")) %>%
gather(intervention, interv_val, starts_with("values.inter")) %>%
filter(grepl(".value", name)) %>%
filter(grepl("node", path)) %>%
mutate(variable = gsub("values.", "", name)) %>%
mutate(variable = gsub(".value", "", variable)) %>%
distinct(contact.name, uuid, name, .keep_all = TRUE) %>%
select(id, uuid, contact.uuid, variable, name_val, created_on, modified_on) %>%
arrange(id, created_on) # optional wide %>% spread(variable, name_val)
Awesome, @hrbmstr! I was able to take this and get it the rest of the way. Shall I make an edit to your answer to suggest the method?
– Eric Green
Nov 9 at 20:01
messy as a comment, but replaceglimpse()
with:filter(grepl(".value", name)) %>% filter(grepl("node", path)) %>% mutate(variable = gsub("values.", "", name)) %>% mutate(variable = gsub(".value", "", variable)) %>% distinct(contact.name, uuid, name, .keep_all = TRUE) %>% select(id, uuid, contact.uuid, variable, name_val, created_on, modified_on) %>% arrange(id, created_on) # optional wide %>% spread(variable, name_val)
– Eric Green
Nov 9 at 20:30
1
definitely go ahead and edit it. just glad it helped. i've had my share of gnarly json in the past.
– hrbrmstr
Nov 9 at 20:37
add a comment |
up vote
2
down vote
accepted
Does this get you any further along? (This is a gnarly structure):
library(tidyverse)
x <- (jsonlite::fromJSON("/Users/hrbrmstr/r7/gh/labs-research/2018-11-portland-ciso-event/example.json"))
jsonlite::stream_out(x$results, con = gzfile("ex-res.json.gz"))
y <- ndjson::stream_in("ex-res.json.gz", "tbl")
gather(y, path, path_val, starts_with("path")) %>%
gather(flow, flow_val, starts_with("flow")) %>%
gather(name, name_val, starts_with("values.pdep")) %>%
gather(intervention, interv_val, starts_with("values.inter")) %>%
glimpse()
## Observations: 87,696
## Variables: 18
## $ contact.name <chr> "Person 1", "Person 2", "Person 1", "Person 2", "Person 1", "Person 2", "Person 1", "Person 2"...
## $ contact.uuid <chr> "k0dcjs", "rd3jfui", "k0dcjs", "rd3jfui", "k0dcjs", "rd3jfui", "k0dcjs", "rd3jfui", "k0dcjs", ...
## $ created_on <chr> "2016-02-08T07:00:15.093813Z", "2016-02-08T07:00:15.093813Z", "2016-02-08T07:00:15.093813Z", "...
## $ id <dbl> 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235...
## $ modified_on <chr> "2016-02-09T04:42:54.812323Z", "2016-02-08T08:09:51.545160Z", "2016-02-09T04:42:54.812323Z", "...
## $ responded <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE...
## $ start.uuid <chr> "dnxh4g", "kfj4dsi", "dnxh4g", "kfj4dsi", "dnxh4g", "kfj4dsi", "dnxh4g", "kfj4dsi", "dnxh4g", ...
## $ uuid <chr> "esn4dk", "qask9dj", "esn4dk", "qask9dj", "esn4dk", "qask9dj", "esn4dk", "qask9dj", "esn4dk", ...
## $ exit_type <chr> NA, "completed", NA, "completed", NA, "completed", NA, "completed", NA, "completed", NA, "comp...
## $ exited_on <chr> NA, "2016-02-08T08:09:51.544998Z", NA, "2016-02-08T08:09:51.544998Z", NA, "2016-02-08T08:09:51...
## $ path <chr> "path.0.node", "path.0.node", "path.0.time", "path.0.time", "path.1.node", "path.1.node", "pat...
## $ path_val <chr> "ecb4cb11-6cca-4791-a950-c448e9300846", "ecb4cb11-6cca-4791-a950-c448e9300846", "2016-02-08T07...
## $ flow <chr> "flow.name", "flow.name", "flow.name", "flow.name", "flow.name", "flow.name", "flow.name", "fl...
## $ flow_val <chr> "weeklyratings", "weeklyratings", "weeklyratings", "weeklyratings", "weeklyratings", "weeklyra...
## $ name <chr> "values.pdeps1.category", "values.pdeps1.category", "values.pdeps1.category", "values.pdeps1.c...
## $ name_val <chr> "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 -...
## $ intervention <chr> "values.intervention", "values.intervention", "values.intervention", "values.intervention", "v...
## $ interv_val <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA...
Full approach:
gather(y, path, path_val, starts_with("path")) %>%
gather(flow, flow_val, starts_with("flow")) %>%
gather(name, name_val, starts_with("values.pdep")) %>%
gather(intervention, interv_val, starts_with("values.inter")) %>%
filter(grepl(".value", name)) %>%
filter(grepl("node", path)) %>%
mutate(variable = gsub("values.", "", name)) %>%
mutate(variable = gsub(".value", "", variable)) %>%
distinct(contact.name, uuid, name, .keep_all = TRUE) %>%
select(id, uuid, contact.uuid, variable, name_val, created_on, modified_on) %>%
arrange(id, created_on) # optional wide %>% spread(variable, name_val)
Awesome, @hrbmstr! I was able to take this and get it the rest of the way. Shall I make an edit to your answer to suggest the method?
– Eric Green
Nov 9 at 20:01
messy as a comment, but replaceglimpse()
with:filter(grepl(".value", name)) %>% filter(grepl("node", path)) %>% mutate(variable = gsub("values.", "", name)) %>% mutate(variable = gsub(".value", "", variable)) %>% distinct(contact.name, uuid, name, .keep_all = TRUE) %>% select(id, uuid, contact.uuid, variable, name_val, created_on, modified_on) %>% arrange(id, created_on) # optional wide %>% spread(variable, name_val)
– Eric Green
Nov 9 at 20:30
1
definitely go ahead and edit it. just glad it helped. i've had my share of gnarly json in the past.
– hrbrmstr
Nov 9 at 20:37
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Does this get you any further along? (This is a gnarly structure):
library(tidyverse)
x <- (jsonlite::fromJSON("/Users/hrbrmstr/r7/gh/labs-research/2018-11-portland-ciso-event/example.json"))
jsonlite::stream_out(x$results, con = gzfile("ex-res.json.gz"))
y <- ndjson::stream_in("ex-res.json.gz", "tbl")
gather(y, path, path_val, starts_with("path")) %>%
gather(flow, flow_val, starts_with("flow")) %>%
gather(name, name_val, starts_with("values.pdep")) %>%
gather(intervention, interv_val, starts_with("values.inter")) %>%
glimpse()
## Observations: 87,696
## Variables: 18
## $ contact.name <chr> "Person 1", "Person 2", "Person 1", "Person 2", "Person 1", "Person 2", "Person 1", "Person 2"...
## $ contact.uuid <chr> "k0dcjs", "rd3jfui", "k0dcjs", "rd3jfui", "k0dcjs", "rd3jfui", "k0dcjs", "rd3jfui", "k0dcjs", ...
## $ created_on <chr> "2016-02-08T07:00:15.093813Z", "2016-02-08T07:00:15.093813Z", "2016-02-08T07:00:15.093813Z", "...
## $ id <dbl> 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235...
## $ modified_on <chr> "2016-02-09T04:42:54.812323Z", "2016-02-08T08:09:51.545160Z", "2016-02-09T04:42:54.812323Z", "...
## $ responded <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE...
## $ start.uuid <chr> "dnxh4g", "kfj4dsi", "dnxh4g", "kfj4dsi", "dnxh4g", "kfj4dsi", "dnxh4g", "kfj4dsi", "dnxh4g", ...
## $ uuid <chr> "esn4dk", "qask9dj", "esn4dk", "qask9dj", "esn4dk", "qask9dj", "esn4dk", "qask9dj", "esn4dk", ...
## $ exit_type <chr> NA, "completed", NA, "completed", NA, "completed", NA, "completed", NA, "completed", NA, "comp...
## $ exited_on <chr> NA, "2016-02-08T08:09:51.544998Z", NA, "2016-02-08T08:09:51.544998Z", NA, "2016-02-08T08:09:51...
## $ path <chr> "path.0.node", "path.0.node", "path.0.time", "path.0.time", "path.1.node", "path.1.node", "pat...
## $ path_val <chr> "ecb4cb11-6cca-4791-a950-c448e9300846", "ecb4cb11-6cca-4791-a950-c448e9300846", "2016-02-08T07...
## $ flow <chr> "flow.name", "flow.name", "flow.name", "flow.name", "flow.name", "flow.name", "flow.name", "fl...
## $ flow_val <chr> "weeklyratings", "weeklyratings", "weeklyratings", "weeklyratings", "weeklyratings", "weeklyra...
## $ name <chr> "values.pdeps1.category", "values.pdeps1.category", "values.pdeps1.category", "values.pdeps1.c...
## $ name_val <chr> "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 -...
## $ intervention <chr> "values.intervention", "values.intervention", "values.intervention", "values.intervention", "v...
## $ interv_val <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA...
Full approach:
gather(y, path, path_val, starts_with("path")) %>%
gather(flow, flow_val, starts_with("flow")) %>%
gather(name, name_val, starts_with("values.pdep")) %>%
gather(intervention, interv_val, starts_with("values.inter")) %>%
filter(grepl(".value", name)) %>%
filter(grepl("node", path)) %>%
mutate(variable = gsub("values.", "", name)) %>%
mutate(variable = gsub(".value", "", variable)) %>%
distinct(contact.name, uuid, name, .keep_all = TRUE) %>%
select(id, uuid, contact.uuid, variable, name_val, created_on, modified_on) %>%
arrange(id, created_on) # optional wide %>% spread(variable, name_val)
Does this get you any further along? (This is a gnarly structure):
library(tidyverse)
x <- (jsonlite::fromJSON("/Users/hrbrmstr/r7/gh/labs-research/2018-11-portland-ciso-event/example.json"))
jsonlite::stream_out(x$results, con = gzfile("ex-res.json.gz"))
y <- ndjson::stream_in("ex-res.json.gz", "tbl")
gather(y, path, path_val, starts_with("path")) %>%
gather(flow, flow_val, starts_with("flow")) %>%
gather(name, name_val, starts_with("values.pdep")) %>%
gather(intervention, interv_val, starts_with("values.inter")) %>%
glimpse()
## Observations: 87,696
## Variables: 18
## $ contact.name <chr> "Person 1", "Person 2", "Person 1", "Person 2", "Person 1", "Person 2", "Person 1", "Person 2"...
## $ contact.uuid <chr> "k0dcjs", "rd3jfui", "k0dcjs", "rd3jfui", "k0dcjs", "rd3jfui", "k0dcjs", "rd3jfui", "k0dcjs", ...
## $ created_on <chr> "2016-02-08T07:00:15.093813Z", "2016-02-08T07:00:15.093813Z", "2016-02-08T07:00:15.093813Z", "...
## $ id <dbl> 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235, 1234, 1235...
## $ modified_on <chr> "2016-02-09T04:42:54.812323Z", "2016-02-08T08:09:51.545160Z", "2016-02-09T04:42:54.812323Z", "...
## $ responded <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE...
## $ start.uuid <chr> "dnxh4g", "kfj4dsi", "dnxh4g", "kfj4dsi", "dnxh4g", "kfj4dsi", "dnxh4g", "kfj4dsi", "dnxh4g", ...
## $ uuid <chr> "esn4dk", "qask9dj", "esn4dk", "qask9dj", "esn4dk", "qask9dj", "esn4dk", "qask9dj", "esn4dk", ...
## $ exit_type <chr> NA, "completed", NA, "completed", NA, "completed", NA, "completed", NA, "completed", NA, "comp...
## $ exited_on <chr> NA, "2016-02-08T08:09:51.544998Z", NA, "2016-02-08T08:09:51.544998Z", NA, "2016-02-08T08:09:51...
## $ path <chr> "path.0.node", "path.0.node", "path.0.time", "path.0.time", "path.1.node", "path.1.node", "pat...
## $ path_val <chr> "ecb4cb11-6cca-4791-a950-c448e9300846", "ecb4cb11-6cca-4791-a950-c448e9300846", "2016-02-08T07...
## $ flow <chr> "flow.name", "flow.name", "flow.name", "flow.name", "flow.name", "flow.name", "flow.name", "fl...
## $ flow_val <chr> "weeklyratings", "weeklyratings", "weeklyratings", "weeklyratings", "weeklyratings", "weeklyra...
## $ name <chr> "values.pdeps1.category", "values.pdeps1.category", "values.pdeps1.category", "values.pdeps1.c...
## $ name_val <chr> "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 - 7", "0 -...
## $ intervention <chr> "values.intervention", "values.intervention", "values.intervention", "values.intervention", "v...
## $ interv_val <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA...
Full approach:
gather(y, path, path_val, starts_with("path")) %>%
gather(flow, flow_val, starts_with("flow")) %>%
gather(name, name_val, starts_with("values.pdep")) %>%
gather(intervention, interv_val, starts_with("values.inter")) %>%
filter(grepl(".value", name)) %>%
filter(grepl("node", path)) %>%
mutate(variable = gsub("values.", "", name)) %>%
mutate(variable = gsub(".value", "", variable)) %>%
distinct(contact.name, uuid, name, .keep_all = TRUE) %>%
select(id, uuid, contact.uuid, variable, name_val, created_on, modified_on) %>%
arrange(id, created_on) # optional wide %>% spread(variable, name_val)
edited Nov 9 at 20:59
Eric Green
2,41753163
2,41753163
answered Nov 9 at 19:32
hrbrmstr
59.8k685146
59.8k685146
Awesome, @hrbmstr! I was able to take this and get it the rest of the way. Shall I make an edit to your answer to suggest the method?
– Eric Green
Nov 9 at 20:01
messy as a comment, but replaceglimpse()
with:filter(grepl(".value", name)) %>% filter(grepl("node", path)) %>% mutate(variable = gsub("values.", "", name)) %>% mutate(variable = gsub(".value", "", variable)) %>% distinct(contact.name, uuid, name, .keep_all = TRUE) %>% select(id, uuid, contact.uuid, variable, name_val, created_on, modified_on) %>% arrange(id, created_on) # optional wide %>% spread(variable, name_val)
– Eric Green
Nov 9 at 20:30
1
definitely go ahead and edit it. just glad it helped. i've had my share of gnarly json in the past.
– hrbrmstr
Nov 9 at 20:37
add a comment |
Awesome, @hrbmstr! I was able to take this and get it the rest of the way. Shall I make an edit to your answer to suggest the method?
– Eric Green
Nov 9 at 20:01
messy as a comment, but replaceglimpse()
with:filter(grepl(".value", name)) %>% filter(grepl("node", path)) %>% mutate(variable = gsub("values.", "", name)) %>% mutate(variable = gsub(".value", "", variable)) %>% distinct(contact.name, uuid, name, .keep_all = TRUE) %>% select(id, uuid, contact.uuid, variable, name_val, created_on, modified_on) %>% arrange(id, created_on) # optional wide %>% spread(variable, name_val)
– Eric Green
Nov 9 at 20:30
1
definitely go ahead and edit it. just glad it helped. i've had my share of gnarly json in the past.
– hrbrmstr
Nov 9 at 20:37
Awesome, @hrbmstr! I was able to take this and get it the rest of the way. Shall I make an edit to your answer to suggest the method?
– Eric Green
Nov 9 at 20:01
Awesome, @hrbmstr! I was able to take this and get it the rest of the way. Shall I make an edit to your answer to suggest the method?
– Eric Green
Nov 9 at 20:01
messy as a comment, but replace
glimpse()
with: filter(grepl(".value", name)) %>% filter(grepl("node", path)) %>% mutate(variable = gsub("values.", "", name)) %>% mutate(variable = gsub(".value", "", variable)) %>% distinct(contact.name, uuid, name, .keep_all = TRUE) %>% select(id, uuid, contact.uuid, variable, name_val, created_on, modified_on) %>% arrange(id, created_on) # optional wide %>% spread(variable, name_val)
– Eric Green
Nov 9 at 20:30
messy as a comment, but replace
glimpse()
with: filter(grepl(".value", name)) %>% filter(grepl("node", path)) %>% mutate(variable = gsub("values.", "", name)) %>% mutate(variable = gsub(".value", "", variable)) %>% distinct(contact.name, uuid, name, .keep_all = TRUE) %>% select(id, uuid, contact.uuid, variable, name_val, created_on, modified_on) %>% arrange(id, created_on) # optional wide %>% spread(variable, name_val)
– Eric Green
Nov 9 at 20:30
1
1
definitely go ahead and edit it. just glad it helped. i've had my share of gnarly json in the past.
– hrbrmstr
Nov 9 at 20:37
definitely go ahead and edit it. just glad it helped. i've had my share of gnarly json in the past.
– hrbrmstr
Nov 9 at 20:37
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53231968%2fparse-json-with-embedded-lists-into-semi-long-dataframe%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