You are not logged in.
Hi!
Given json such as this:
{
"trees":
[
{
"Name": "Tree 1"
"Branches":
[
{
"Name": "Branch 1"
"Leaves":
[
{
"Name": "Leaf 1"
},
{
"Name": "Leaf 2"
}
]
},
{
"Name": "Branch 2"
"Leaves":
[
{
"Name": "Leaf 3"
},
{
"Name": "Leaf 4"
}
]
}
]
}
]
}
Is there a function to search for leaf based on its path and name?
Something defined similar to:
function findDVInPathByValue(const APath, AValue: string; out DV: PDocVariantData; var SearchStartIndex: Integer): Boolean;
And called like so:
SearchIndex := 0;
while
Forest.findDVInPathByValue('trees.branches.leaves.name', 'Leaf 3', Leaf, SearchIndex)
do begin
// do stuff with each leaf named "Leaf 3"
end
I know how to iterate the trees and then branches and leaves.
V := _json('...');
Trees := _safe(V.Trees);
for Idx := 0 to Trees.Count - 1 do
begin
Branch := _Safe(Trees.Values[Idx].Branches);
for Idx2 := 0 to Branch.Count - 1 do
begin
Leaves := _Safe(Branch.Values[Idx2].Leaves);
if Leaves.GetDocVariantByProp('Name', 'Leaf 3', False, Leaf) then // only saves the last level
begin
...
I was just wondering if there is something more appropriate/convenient already available.
Offline
I think GetValueByPath should be OK.
Offline
No, GetValueByPath returns value of (only first?) item matching the path.
But I already know the value. Instead I need to find the object that has given value.
Offline
There's problably nothing ready like that, SearchItemByProp only looks in first level.
Offline
Is there a function to search for leaf based on its path and name?
My first question would be: How big is the JSON file? If the JSON file is not too large, I would deserialize everything into a record structure and then pick out the data you want. This is simple, can be implemented quickly and you have full access to all data if more is needed.
With best regards
Thomas
Offline
I just thought it would be useful function to have and i asked because i could not find anything like it.
I already solved my specific problem with dumb nested iterations. It's fast but stupid to write. I had 5 levels of nesting.
So please take it as a feature suggestion.
Thank you Igor and Thomas!
Offline