web
You’re offline. This is a read only version of the page.
close
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 100
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;
I have the same question (0)
  • Gerardo Rentería García Profile Picture
    25,157 Most Valuable Professional on at
  • Verified answer
    Marcus Halbauer Profile Picture
    100 on at
    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! 

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,229

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,867 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,153 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans