R - Filtering list of nested lists and finding back a higher level key -
i working on parsing json containing api info several accounts. data used in api wrapper.
[ { "geo":"au", "api": [ {"type":"base_url", "value":"https://api.com"}, {"type":"key", "value":"f18"}, {"type":"secret", "value":"569"} ], "accounts": [ {"name":"client_aqe","id":"8765","inventory":"all","currency":"aud"} ] },{ "geo":"eu", "api": [ {"type":"base_url", "value":"https://api.com"}, {"type":"key", "value":"p8c6043"}, {"type":"secret", "value":"983df5"} ], "accounts": [ {"name":"client_uyt","id":"098765","inventory":"all","currency":"gbp"}, {"name":"client_wer","id":"09098","inventory":"all","currency":"gbp"}, {"name":"client_oip","id":"234543","inventory":"all","currency":"eur"} ] } ] when user give me "account name" doesn't specify "geo", to:
- check if client appears once (this can done function below, using
rlist)
only_one_client <- function(account_name) { return(fromjson(file=api_info_path) %>% list.map(accounts) %>% unlist(recursive=f) %>% list.mapv(name) %>% table() %>% extract(account_name) == 1)) }
- if client appears once, find "geo" appears in
this cannot find way do
you transform json dataframe holding each accounts names , corresponding geo, if data result of fromjson(file=api_info_path) do:
res <- as.data.frame(do.call(rbind,lapply(data,function(x) cbind(x[["geo"]],do.call(rbind,x[["accounts"]]))))) colnames(res) <- c("geo",colnames(res[,-1])) res <- merge(res,as.data.frame(table(res$name)),by.x="name",by.y="var1") # name geo id inventory currency freq #1 client_aqe au 8765 aud 1 #2 client_oip eu 234543 eur 1 #3 client_uyt eu 098765 gbp 1 #4 client_wer eu 09098 gbp 1 this loops through list, rbind accounts , adds geo each row.
you can use table in function check how many times name appears , geo comes from.
Comments
Post a Comment