convert list to array in R - r

I am putting some results together in a nested list (with arrays). The expected results must be EXACTLY as follows:
{
"item1": "TEXT",
"item2": "MORE TEXT",
"item3": [
"STILL TEXT"
],
"item4": [
"TEXT AGAIN"
],
"values": [
{
"start": 0,
"end": 99
}
]
}
I put all my results together like this:
listToJson <- c(list(item1 = "TEXT",
item2 = "MORE TEXT",
item3 = "STILL TEXT",
item4 = "TEXT AGAIN",
values = list(start = 99,
end = 0)))
write_json(listToJson, path = "test.json", auto_unbox = TRUE , null = "null")
The problem is that the results doesn't have array elements (see below). item3 and item4 should be arrays. How can I change my code to get the expected results in that exact format?
{
"item1":"TEXT",
"item2":"MORE TEXT",
"item3":"STILL TEXT",
"item4":"TEXT AGAIN",
"values":{
"start":99,
"end":0}
}

You can just use as.array for those specific items.
library(jsonlite)
listToJson <- c(
list(
item1 = "TEXT",
item2 = "MORE TEXT",
item3 = as.array("STILL TEXT"),
item4 = as.array("TEXT AGAIN"),
values = as.array(list(start = 99,
end = 0))
)
)
write_json(listToJson, path = "test.json", auto_unbox = TRUE , null = "null")
Output
{
"item1":"TEXT",
"item2":"MORE TEXT",
"item3":[
"STILL TEXT"
],
"item4":[
"TEXT AGAIN"
],
"values":[
{
"start":0,
"end":99
}
]
}

Related

R: Nested data.table to JSON

I want to get from a data.table like this
temp <- data.table(data = list(data.table(a = 1:2,b=1:2)), type = "A")
data
type
<data.table[2x2]>
A
to a JSON like this
{
"group":
{
"data": [
{
"a": 1,
"b": 1
},
{
"a": 2,
"b": 2
}
],
"type": "A"
}
}
The Problem is I always end up with an additional array "[" for group.
What I have tried is tidyr::nest and
temp2 <- temp[, list(group=list(.SD))]
jsonlite::toJSON(temp2,pretty = TRUE, auto_unbox = TRUE)
temp3 <- temp[, (list(group=list(as.list(.SD))))]
jsonlite::toJSON(temp3,pretty = TRUE, auto_unbox = TRUE)
Is there an "easy" solution for my problem?
Thanks
edit more complex example
temp <-
data.table(
id1 = 1:6,
id2 = c(rep("A", 2), rep("B", 2), rep("C", 2)),
data = rep(list(data.table(
a = 1:2, b = 1:2
)), 6),
type = "test"
)
nest1 <- temp[, list(list(.SD)),by=.(id1,id2)] %>% setnames("V1","group")
nest1[, type:="B"]
nest2 <- nest1[, list(list(.SD)),by=.(id2)] %>% setnames("V1","data")
nest2[, type:="C"]
nest3 <- nest2[, list(list(.SD)),by=.(id2)] %>% setnames("V1","group")
jsonlite::toJSON(nest3, pretty = TRUE)
desired output (shortend):
Group should only contain objects and no arrays
[
{
"id2": "A",
"group": {
"data": [
{
"id1": 1,
"group": {
"data": [
{
"a": 1,
"b": 1
},
{
"a": 2,
"b": 2
}
],
"type": "test"
},
"type": "B"
},
{
"id1": 2,
"group": {
"data": [
{
"a": 1,
"b": 1
},
{
"a": 2,
"b": 2
}
],
"type": "test"
},
"type": "B"
}
],
"type": "C"
}
},
{
"id2": "B",
"group": {
"data": [],
"type": "C"
}
}
]
We could use jq to do the unboxing as a post-processing step, since jsonlite doesn't seem to allow for this specific use case:
jsonlite::toJSON(nest3, pretty = TRUE) %>%
jqr::jq('walk(if type=="array" and length==1 then .[0] else . end)')
The jq bit is taken from jq ~ is there a better way to collapse single object arrays?

R: Assign vector element as list variable name

Here is the end result that I would like to achieve:
json_creative_pause = list("47770124" =
list(patch = list(
`$set` = list (
status = "PAUSED"
))))
The issue is that I would like to create this same structure dynamically for multiple ids. I have this variable:
creative_ids_to_pause = c("75196186", "78369656", "80050466")
And I would like to recreate the same structure for each creative id. But running such code doesn't really work:
json_creative_pause = list(get(creative_ids_to_pause[1]) =
list(patch = list(
`$set` = list (
status = "PAUSED"
))))
Does anyone know how to assign a vector element as a variable name inside of a list?
Thank you for your help!
Arben
We can use setNames
setNames(list(patch = list(
`$set` = list (
status = "PAUSED"
))), creative_ids_to_pause[1])
#$`75196186`
#$`75196186`$`$set`
#$`75196186`$`$set`$status
#[1] "PAUSED"
Or another option is dplyr::lst
dplyr::lst(!! creative_ids_to_pause[1] :=
list(patch = list(
`$set` = list (
status = "PAUSED"
))))
#$`75196186`
#$`75196186`$patch
#$`75196186`$patch$`$set`
#$`75196186`$patch$`$set`$status
#[1] "PAUSED"
I also found this solution:
creative_ids_to_pause = c("75196186", "78369656", "80050466")
unnamed_json_structure = list(
patch = list(
`$set` = list(
status = "PAUSED"
)))
list_all_creative_ids = list()
for(i in 1:length(creative_ids_to_pause)){
list_all_creative_ids[[creative_ids_to_pause[i]]] = unnamed_json_structure
}
list_all_creative_ids %>%
toJSON(pretty = TRUE, auto_unbox = TRUE)
Result
"75196186": {
"patch": {
"$set": {
"status": "PAUSED"
}
}
},
"78369656": {
"patch": {
"$set": {
"status": "PAUSED"
}
}
},
"80050466": {
"patch": {
"$set": {
"status": "PAUSED"
}
}
}
}

Creating nested list in R

I have a data frame like below:
df <- data.frame(child = c('item3-1-1','item3-1-2','item3-2','item3-1','item2-1','item2-2','item1'),parent = c('item3-1','item3-1','item3','item3','item2','item2',''))
I want to convert this dataframe in below format:
choices <-
list(
list(id = 1, title = "item1"),
list(id = 2, title = "item2",
subs = list(
list(id = 21, title = "item2-1"),
list(id = 22, title = "item2-2")
)
),
list(id = 3, title = "item3",
subs = list(
list(id = 31, title = "item3-1", isSelectable = FALSE,
subs = list(
list(id = 311, title = "item3-1-1"),
list(id = 312, title = "item3-1-2")
)
),
list(id = 32, title = "item3-2")
)
)
)
I need the nested list with option of 'subs' to traverse the tree drop-down list.
Is there any function or method by which I can achieve this as I have huge dataset.
Here is a function which generates the nested list. The id are not the same but anyway we do not use them in ComboTree (but they are required).
dat <- data.frame(
item = c("item1", "item2", "item2-1", "item2-2", "item3", "item3-1",
"item3-1-1", "item3-1-2", "item3-2"),
parent = c("root", "root", "item2", "item2", "root", "item3",
"item3-1", "item3-1", "item3"),
stringsAsFactors = FALSE
)
makeChoices <- function(dat){
f <- function(parent, id = "id"){
i <- match(parent, dat$item)
title <- dat$item[i]
subs <- dat$item[dat$parent==title]
if(length(subs)){
list(
title = title,
id = paste0(id,"-",i),
subs = lapply(subs, f, id = paste0(id,"-",i))
)
}else{
list(title = title, id = paste0(id,"-",i))
}
}
lapply(dat$item[dat$parent == "root"], f)
}
choices <- makeChoices(dat)
> jsonlite::toJSON(choices, auto_unbox = TRUE, pretty = TRUE)
[
{
"title": "item1",
"id": "id-1"
},
{
"title": "item2",
"id": "id-2",
"subs": [
{
"title": "item2-1",
"id": "id-2-3"
},
{
"title": "item2-2",
"id": "id-2-4"
}
]
},
{
"title": "item3",
"id": "id-5",
"subs": [
{
"title": "item3-1",
"id": "id-5-6",
"subs": [
{
"title": "item3-1-1",
"id": "id-5-6-7"
},
{
"title": "item3-1-2",
"id": "id-5-6-8"
}
]
},
{
"title": "item3-2",
"id": "id-5-9"
}
]
}
]

Flatten nested JSON to dataframe in R

I am trying to flatten a nested JSON file from within R,
Here is my current code
library(jsonlite)
json_file <- "json file"
json_data = fromJSON(json_file, flatten = FALSE)
flat_data = as.data.frame(json_data)
However i am getting the below error
flat_data = as.data.frame(json_data)
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, :
arguments imply differing number of rows: 1, 13, 3201
Here is a sample of my JSON structure
{
"RIDE":{
"STARTTIME":"2020\/01\/05 22:27:49 UTC ",
"RECINTSECS":1,
"DEVICETYPE":"Garmin FR735XT ",
"IDENTIFIER":" ",
"TAGS":{
"Aerobic Training Effect":"3.8 ",
"Athlete":"Chuck Finley",
"Data":" ",
"Device":"Garmin",
"Device Info":"HR Garmin 2327",
"File Format":" ",
"Filename":"2020_01_06_06_27_49.json ",
"Month":"January ",
"Performance Condition":"-5 ",
"Recovery Time":" ",
"Source Filename":"A1662750_2020_01_06_06_27_49.gz ",
"Sport":"Run ",
"SubSport":" ",
"VO2max detected":"61.7 ",
"Weekday":"Mon ",
"Workout Code":" ",
"Year":"2020 "
},
"INTERVALS":[
{ "NAME":"Lap 1 ", "START": 0, "STOP": 249, "COLOR":"#000000", "PTEST":"false" },
{ "NAME":"Lap 2 ", "START": 250, "STOP": 504, "COLOR":"#000000", "PTEST":"false" }
],
"SAMPLES":[
{ "SECS":0, "KM":0, "KPH":0, "HR":104, "ALT":14, "LAT":-40.402758436, "LON":175.0371112, "SLOPE":0, "LRBALANCE":0, "RCAD":109.5, "RVERT":0.47, "RCON":273 },
{ "SECS":1, "KM":0.00056, "KPH":0, "HR":104, "ALT":14, "LAT":-40.402758436, "LON":175.0371112, "SLOPE":0, "LRBALANCE":24.87, "RCAD":109.5, "RVERT":0.47, "RCON":273 }
],
"XDATA":[
{
"NAME" : "EXTRA",
"VALUES" : [ "STANCETIMEPERCENT", "VERTICALRATIO", "STEPLENGTH", "FIELD_88", "ACTIVITYTYPE", "PERFORMANCECONDITION" ],
"UNITS" : [ "", "", "", "", "", "" ],
"SAMPLES" : [
{ "SECS":1, "KM":0, "VALUES":[ 48.5, 0, 0, 300, 1, 0 ] },
{ "SECS":2, "KM":0, "VALUES":[ 48.5, 4.96, 891, 300, 1, 0 ] }
]
}
]
}
}
I am quite new to R, so any advise would be appreciated.
Just change the file name. Json file should be .json
json_file <- "json file.json"

Mscgen grammar for PegKit

Is anyone aware of an existing grammar definition of Mscgen syntax that will work with PegKit? I had a look in the "res" folder but most of those don't seem to work.
Here is a sample
# MSC for some fictional process
msc {
hscale = "2";
a,b,c;
a->b [ label = "ab()" ] ;
b->c [ label = "bc(TRUE)"];
c=>c [ label = "process(1)" ];
c=>c [ label = "process(2)" ];
...;
c=>c [ label = "process(n)" ];
c=>c [ label = "process(END)" ];
a<<=c [ label = "callback()"];
--- [ label = "If more to run", ID="*" ];
a->a [ label = "next()"];
a->c [ label = "ac1()\nac2()"];
b<-c [ label = "cb(TRUE)"];
b->b [ label = "stalled(...)"];
a<-b [ label = "ab() = FALSE"];
}

Resources