#1 2022-11-22 16:52:26

Lauri
Member
From: Under a rock
Registered: 2020-12-05
Posts: 25

Recursive search in DocVariant given path and value

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

#2 2022-11-22 17:22:03

igors233
Member
Registered: 2012-09-10
Posts: 241

Re: Recursive search in DocVariant given path and value

I think GetValueByPath should be OK.

Offline

#3 2022-11-22 19:13:13

Lauri
Member
From: Under a rock
Registered: 2020-12-05
Posts: 25

Re: Recursive search in DocVariant given path and value

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

#4 2022-11-23 07:21:11

igors233
Member
Registered: 2012-09-10
Posts: 241

Re: Recursive search in DocVariant given path and value

There's problably nothing ready like that, SearchItemByProp only looks in first level.

Offline

#5 2022-11-23 19:21:00

tbo
Member
Registered: 2015-04-20
Posts: 353

Re: Recursive search in DocVariant given path and value

Lauri wrote:

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

#6 2022-11-24 17:06:19

Lauri
Member
From: Under a rock
Registered: 2020-12-05
Posts: 25

Re: Recursive search in DocVariant given path and value

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

Board footer

Powered by FluxBB