Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

"Data not represent a valid json token" in Production Environment but not in Sandbox Environment

(1) ShareShare
ReportReport
Posted on by 93
Hello all,
 
I need your help regarding a error message which appears in my production environment but not in the sandbox environment.
I am using a codeunit to fetch the latest currency exchange rate for all our currencies from the Swedish Central Bank (Riksbanken).
The codeunit uses a GET REST API and is run Monday to Friday.
 
However, since I updated both the production environment and the sandbox environment to Microsoft BC version 23.3,
the identical codeunits (same version etc) and job queue entry does not work in the production environment, but works in the sandbox environment.
When I run the codeunit in production environment, I get the following error message:
/Data no represent a valid JSON token./
Please see the code below.
 
How can this discrepancy between production and sandbox environment be?
How do I fix the issue?
 
Thank you for your help.
 
codeunit 50750 /Riksbankens Valutakurser ACU/
{
    /*Retrieves the latest currency exchange rates from Sveriges Riksbank.
    Link to Sveriges Riksbanks API-portal: https://developer.api.riksbank.se/apis
    Tutorial: https://www.navuser.com/how-to-call-a-rest-api-get-request-in-al/
    */
 
    TableNo = /Job Queue Entry/;
 
    var
        GeneralLedgerSetup: Record /General Ledger Setup/;
        Currency: Record Currency;
        CurrencyExchangeRate: Record /Currency Exchange Rate/;
        BaseURL: Label 'https:/api.riksbank.se/swea/v1/Observations/Latest/';
        MyHTTPClient: HttpClient;
        HTTPResponseMessage: HttpResponseMessage;
        RequestFailError: Label 'Unable to process the request through the API.';
        InvalidResponseError: Label 'Invalid response.';
 
    trigger OnRun();
    var
        JsonString: Text;
        JsonTokenVar: JsonToken;
        JsonObj: JsonObject;
        CurrencyExchangeRateDate: Date;
        CurrencyExchangeRateValue: Decimal;
    begin
        GeneralLedgerSetup.GET();
        GeneralLedgerSetup.TESTFIELD(/LCY Code/, 'SEK');
        //Loops through all the currencies.
        if Currency.FindSet() then begin
            repeat
                JsonString := GetCurrencyDataByAPI();
                //if not JsonObj.ReadFrom(JsonString) then
                //  Error(InvalidResponseError);
                JsonTokenVar.ReadFrom(JsonString);
                if JsonTokenVar.IsObject then begin
                    //Retrieves the relevant currency data from the retrieved data from Sveriges Riksbank.
                    CurrencyExchangeRateDate := GetJsonObjectValueFromToken('date', JsonTokenVar).AsDate();
                    CurrencyExchangeRateValue := GetJsonObjectValueFromToken('value', JsonTokenVar).AsDecimal();
                    CurrencyExchangeRateValue := Round(CurrencyExchangeRateValue, 0.01);
                    InsertCurrencyExchangeRate(CurrencyExchangeRateDate, CurrencyExchangeRateValue);
                end;
            until Currency.Next() = 0;
        end;
    end;
 
    local procedure InsertCurrencyExchangeRate(CurrencyExchangeRateDate: Date; CurrencyExchangeRateValue: Decimal);
    begin
        //Inserts the currency data.
        CurrencyExchangeRate.Init();
        CurrencyExchangeRate./Currency Code/ := Currency.Code;
        CurrencyExchangeRate./Starting Date/ := CurrencyExchangeRateDate;
 
        CurrencyExchangeRate./Exchange Rate Amount/ := 1;
        CurrencyExchangeRate./Adjustment Exch. Rate Amount/ := 1;
 
        CurrencyExchangeRate./Relational Exch. Rate Amount/ := CurrencyExchangeRateValue;
        CurrencyExchangeRate./Relational Adjmt Exch Rate Amt/ := CurrencyExchangeRateValue;
        if not CurrencyExchangeRate.Insert() then
            CurrencyExchangeRate.Modify();
    end;
 
    local procedure GetCurrencyDataByAPI() JsonString: Text;
    begin
        //Retrieves the currency daata from Sveriges Riksbank and returns it as a JsonString.
        if not MyHTTPClient.Get(BaseURL + GeneralLedgerSetup./LCY Code/ + Currency.Code + 'PMI', HTTPResponseMessage) then
            Error(RequestFailError);
        HTTPResponseMessage.Content().ReadAs(JsonString);
    end;
 
    local procedure GetJsonObjectValueFromToken(Parameter: Text; JsonTokenVar: JsonToken): JsonValue;
    begin
        //Getting values of the parameters in the JSON object.
        JsonTokenVar.AsObject().Get(Parameter, JsonTokenVar);
        exit(JsonTokenVar.AsValue());
    end;
  • Verified answer
    Marcus Halbauer Profile Picture
    Marcus Halbauer 93 on at
    "Data not represent a valid json token" in Production Environment but not in Sandbox Environment
    Hello @gdrenteria,
     
    Thank you very much for your feedback.
    I found the solution.
    Somehow, with my clumsy fingers, I had managed to change the currency code of one of the currencies, hence the error message.
    So when I typed in the correct currency code, the codeunit worked as normal.
    Case closed! 
  • gdrenteria Profile Picture
    gdrenteria 14,483 Most Valuable Professional on at
    "Data not represent a valid json token" in Production Environment but not in Sandbox Environment

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans