I have a JSON file with very deep hierarchy (e.g. https://raw.githubusercontent.com/APSIMInitiative/ApsimX/master/Models/Resources/Wheat.json)
# read data
library(jsonlite)
m <- read_json('https://raw.githubusercontent.com/APSIMInitiative/ApsimX/master/Models/Resources/Wheat.json')
I would like to find an element with Name == "PotentialBranchingRate".
{
"$type": "Models.Functions.PhaseLookup, Models",
"Name": "PotentialBranchingRate",
"Children": [...]
}
The expected results are to return parental node of "Name": "PotentialBranchingRate".
List of 2
$ $type: chr "Models.Functions.PhaseLookup, Models"
$ Name : chr "PotentialBranchingRate"
$ Children : list ...
I have searched functions in the packages rlist and purrr, but cannot find any ways to do it.
Any suggestions to solve this problem?
library(data.tree)
x <- as.Node(m)
Traverse(x, filterFun = function(.)
identical(GetAttribute(., "Name"), "PotentialBranchingRate")
)
#> [[1]]
#> levelName
#> 1 2
#> 2 °--Children
#> 3 ¦--1
#> 4 ¦ °--Children
#> 5 ¦ °--1
#> 6 ¦ °--Children
#> 7 ¦ °--1
#> 8 ¦ ¦--X
#> 9 ¦ ¦--Y
#> 10 ¦ °--Children
#> 11 °--2
#> 12 °--Children
#> 13 °--1
#> 14 °--Children
#>
#> [[2]]
#> levelName
#> 1 1
#> 2 °--Children
#> 3 °--1
#> 4 ¦--X
#> 5 ¦--Y
#> 6 °--Children
res <- .Last.value
lapply(res, function(.) jsonlite::prettify(rjson::toJSON(as.list(.))))
#> [[1]]
#> {
#> "$type": "Models.Functions.PhaseLookup, Models",
#> "Enabled": true,
#> "IncludeInDocumentation": true,
#> "Name": "PotentialBranchingRate",
#> "ReadOnly": false,
#> "Children": {
#> "1": {
#> "$type": "Models.Functions.PhaseLookupValue, Models",
#> ....
#>
#> ....... (truncated output for clarity)
#>
#> [[2]]
#> {
#> "$type": "Models.Functions.LinearInterpolationFunction, Models",
#> "Enabled": true,
#> "IncludeInDocumentation": true,
#> "Name": "PotentialBranchingRate",
#> "ReadOnly": false,
#> "XProperty": "[Structure].LeafTipsAppeared",
#> "Children": {
#> "1": {
#> "$type": "Models.Functions.XYPairs, Models",
#> "Enabled": true,
#> "IncludeInDocumentation": true,
#> "Name": "XYPairs",
#> "ReadOnly": false,
#> "X": {
#> "1": 1,
#> "2": 2,
#> "3": 3,
#> "4": 4,
#> "5": 5,
#> "6": 6,
#> "7": 7,
#> "8": 8
#> },
#> "Y": {
#> "1": 0,
#> "2": 0,
#> "3": 1,
#> "4": 2,
#> "5": 4,
#> "6": 7,
#> "7": 12,
#> "8": 20
#> },
#> "Children": [
#>
#> ]
#> }
#> }
#> }
#>
Created on 2019-02-21 by the reprex package (v0.2.1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With