Given an example data set:
data = {"region" -> "AA",
"systems" -> {{"name" -> 1,
"sub" -> {{"name" -> "sub11"}, {"name" -> "sub12"}}},
{"name" -> 2,
"sub" -> {{"name" -> "sub21"}, {"name" -> "sub22"}}}}};
I would like to extract the various systems and subsystems attributes. I would prefer a sysntax similar to JsonPath ot XPath.
So for example, I would like to be able to write something similar to:
data."region" (* AA *)
data."systems"[2]."name" (* 2 *)
data."systems"[2]."sub"[2]."name" (* sub22 *)
Filtering is a nice bonus
data."systems"[#."name" == 1 &]."sub"."name" (* {"sub11", "sub12"} *)
How could I make this or a similar syntax work?
Using ReplaceRepeated almost works (just the substitutions have to be written in reverse ie "name" /. "systems"[[2]] instead of the more logical "systems"[[2]] -> "name").
{"region", "name" /. "systems"[[2]], "sub"[[2]] /. "systems"[[2]]} //. data
(* ==> {"AA", 2, {"name" -> "sub22"}} *)
But this fails, as "name" appears on two levels
{"region", "name" /. "systems"[[2]],
"name" /. "sub"[[2]] /. "systems"[[2]]} //. data
(* ==> {"AA", 2, 2} but I would like {"AA", 2, sub22} *)

OptionValue[data, "systems"[[2]] -> "sub"[[2]] -> "name"]easier to read thanOptionValue[ OptionValue[OptionValue[data, "systems"][[2]], "sub"][[2]], "name"]. I guess I would like some sort of Xpath approximation where it is also possible to get nodes either by position or name. – Ajasja Jul 16 '12 at 09:51Compress-Uncompress, and store values in Java as compressed strings, but this induces quite an overhead (I tried in the context of set data structure). – Leonid Shifrin Jul 16 '12 at 11:05