TJSONData.FindPath function
Find data by name.
Declaration
function FindPath(const APath: TJSONStringType): TJSONDataArguments
APath | Path to search for. |
Function result
The found data element, or nil.
Description
FindPath finds a value based on its path. If none is found,
Nil is returned.
The path elements are separated by dots and square brackets, as in object
member notation or array notation. The path is case sensitive.
- For simple values, the path must be empty.
- For JSON objects, a member can be specified using its name, and the object value itself can be retrieved with the empty path.
- For JSON Arrays, the elements can be found based on an array index. The array value itself can be retrieved with the empty path.
The following code will return the value itself, i.e. E will contain the same
element as D:
Var
D,E : TJSONData;
begin
D:=TJSONIntegerNumber.Create(123);
E:=D.FindPath('');
end.
The following code will not return anything:
Var
D,E : TJSONData;
begin
D:=TJSONIntegerNumber.Create(123);
E:=D.FindPath('a');
end.
The following code will return the third element from the array:
Var
D,E : TJSONData;
begin
D:=TJSONArray.Create([1,2,3,4,5]);
E:=D.FindPath('[2]');
Writeln(E.AsJSON);
end.
The output of this program is 3.
The following code returns the element Age from the object:
Var
D,E : TJSONData;
begin
D:=TJSONObject.Create(['Age',23,
'Lastame','Rodriguez',
'FirstName','Roberto']);
E:=D.FindPath('Age');
Writeln(E.AsJSON);
end.
The code will print 23.
Obviously, this can be combined:
Var
D,E : TJSONData;
begin
D:=TJSONObject.Create(['Age',23,
'Names', TJSONObject.Create([
'LastName','Rodriguez',
'FirstName','Roberto'])]);
E:=D.FindPath('Names.LastName');
Writeln(E.AsJSON);
end.
And mixed:
var
D,E : TJSONData;
begin
D:=TJSONObject.Create(['Children',
TJSONArray.Create([
TJSONObject.Create(['Age',23,
'Names', TJSONObject.Create([
'LastName','Rodriguez',
'FirstName','Roberto'])
]),
TJSONObject.Create(['Age',20,
'Names', TJSONObject.Create([
'LastName','Rodriguez',
'FirstName','Maria'])
])
])
]);
E:=D.FindPath('Children[1].Names.FirstName');
Writeln(E.AsJSON);
end.See also
An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.
