Skip to content

Instantly share code, notes, and snippets.

@martonsagi
Created February 7, 2021 21:00
Show Gist options
  • Save martonsagi/dc8077b8be6d3018978eeada0d9334b5 to your computer and use it in GitHub Desktop.
Save martonsagi/dc8077b8be6d3018978eeada0d9334b5 to your computer and use it in GitHub Desktop.
...
/// <summary>
/// Example: JsonStuff1('{ "MyKey": { "ElementValue": "Result" } }', 'MyKey.ElementValue', 'ElementValue');
/// </summary>
/// <param name="SourceObject">JSON as Text</param>
/// <param name="Path">JPath selector</param>
/// <param name="PropertyName">Requested Property name</param>
/// <returns></returns>
local procedure JsonStuff1(SourceObject: Text; Path: Text; PropertyName: Text): Text;
var
TempJsonBuffer: Record "JSON Buffer" temporary;
Result: Text;
begin
TempJsonBuffer.ReadFromText(SourceObject);
if TempJsonBuffer.GetPropertyValueAtPath(Result, PropertyName, Path) then
exit(Result);
end;
/// <summary>
/// Example: JsonStuff2(JsonObjectAsToken, 'MyKey.ElementValue');
/// </summary>
/// <param name="SourceObject">JSON as JsonToken</param>
/// <param name="Path">JPath selector</param>
/// <returns></returns>
local procedure JsonStuff2(SourceObject: JsonToken; Path: Text): Text;
var
TargetToken: JsonToken;
Result: Text;
begin
if (SourceObject.SelectToken(Path, TargetToken)) then
if TargetToken.IsValue() and not TargetToken.AsValue().IsUndefined() then
Result := TargetToken.AsValue().AsText();
exit(Result);
end;
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment