0

After making a Get-Request to an Endpoint, I parse the returned Json String key by key, which works. The problem occurs when I try to convert the returned Date ('createdAt') to Date Type.

The error I receive

Die Konvertierung von Microsoft.Dynamics.Nav.Runtime.NavJsonValue in Microsoft.Dynamics.Nav.Runtime.NavDate ist nicht möglich.

Which translates to something like:

Unable to convert from NavJsonValue to NavDate

The Json I parse

{

    "entryNo": "2",

    "title": "TEST",

    "description": "Test Item",

    "websiteUrl": "Test Url",

    "createdAt": "14.01.2021"

}

Relevant code

_testEntry.CreatedAt := GetJsonToken(jsonObject, 'createdAt').AsValue().AsDate();

local procedure GetJsonToken(jsonObject: JsonObject; tokenKey: Text) jsonToken: JsonToken;
begin
    if not jsonObject.Get(tokenKey, jsonToken) then
        exit;
end;
samuel gast
  • 331
  • 4
  • 17

1 Answers1

0

The date format returned is not a valid JavaScript format, which is what AsDate() expects.

If you control the endpoint you should alter the date format to YYYY-MM-DD.

If you have no control over the endpoint then you need to parse the date value:

local procedure ParseDate(Token: JsonToken): Date
var
    DateParts: List of [Text];
    Year: Integer;
    Month: Integer;
    Day: Integer;
begin
    // Error handling omitted from example
    DateParts := Token.AsValue().AsText().Split('.');
    Evaluate(Day, DateParts.Get(1));
    Evaluate(Month, DateParts.Get(2));
    Evaluate(Year, DateParts.Get(3));
    exit(DMY2Date(Day, Month, Year));
end;
kaspermoerch
  • 16,127
  • 4
  • 44
  • 67