Create JSON file using C/AL Code

navuser1navuser1 Member Posts: 1,329
Dear Sir,

Suppose I'm using standard nav 2016 database & want to create a JSON file on Customer Master tabledata.
So how can I do that using C/AL code ?

Kindly reply.

navuser1
Now or Never

Answers

  • ShaiHuludShaiHulud Member Posts: 228
    Either implement your own, or backport Codeunit 5459 JSON Management from newer NAV and use functions there to generate your JSON
  • kylehardinkylehardin Member Posts: 257
    There are also several useful functions on Table 1236 - backport that too
    Kyle Hardin - ArcherPoint
  • navuser1navuser1 Member Posts: 1,329
    edited 2020-02-11
    I will check
    Now or Never
  • navuser1navuser1 Member Posts: 1,329
    kylehardin wrote: »
    There are also several useful functions on Table 1236 - backport that too

    Which version of NAV I have to look for ?
    Now or Never
  • kylehardinkylehardin Member Posts: 257
    I think that was introduced in NAV 2017. 2018 for sure.
    Kyle Hardin - ArcherPoint
  • navuser1navuser1 Member Posts: 1,329
    I will collect the same. BTW, I just want to know that Nav is able to create JSON thru its C/AL ?
    Now or Never
  • navuser1navuser1 Member Posts: 1,329
    edited 2020-02-12
    code for iteration(Loop) to create a JSON file ?
    Now or Never
  • kylehardinkylehardin Member Posts: 257
    I don't have any sample code for constructing a JSON file, but table 1236 has lots of functions attached for JSON data handling. I've put some sample code below, but this reads data from a JSON file rather than trying to construct a JSON file. I think the process would basically be to add data to the JSON table, and then call a method that exports that as JSON into a BLOB.

    My advice would be to make yourself a docker container with NAV 2018. Pull out all of the objects as text, and see if you can find any example code in those objects that is calling and using Table 1236.

    PROCEDURE BingDistance@1240060001();
    VAR
    TempBLOB@1240060005 : Record 99008535;
    JSONBuffer@1240060010 : TEMPORARY Record 1236;
    HTTPWebRequestMgt@1240060002 : Codeunit 1297;
    WebOutStream@1240060004 : OutStream;
    WebInStream@1240060003 : InStream;
    HTTPStatusCode@1240060008 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpStatusCode";
    ResponseHeaders@1240060006 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.Specialized.NameValueCollection";
    URL@1240060000 : Text;
    Response@1240060007 : Text;
    Mileage@1240060011 : Decimal;
    BEGIN
    URL := 'https://dev.virtualearth.net/REST/v1/Routes/DistanceMatrix?origins=3.98344,-8.20666&destinations=3.78122,-8.39699';
    URL := URL + '&travelMode=driving&startTime=2:00:00-05:00&distanceUnit=mile';

    HTTPWebRequestMgt.Initialize(URL);
    IF NOT GUIALLOWED THEN
    HTTPWebRequestMgt.DisableUI();
    HTTPWebRequestMgt.SetReturnType('application/json');

    CLEAR(TempBLOB);
    CLEAR(WebInStream);
    TempBLOB.Blob.CREATEINSTREAM(WebInStream);

    IF NOT HTTPWebRequestMgt.GetResponse(WebInStream, HTTPStatusCode, ResponseHeaders) THEN BEGIN
    HTTPWebRequestMgt.ProcessFaultResponse(Response);
    ERROR(Response);
    END;

    WebInStream.READTEXT(Response);
    CLEAR(JSONBuffer);
    JSONBuffer.ReadFromText(Response);
    IF JSONBuffer.GetDecimalPropertyValue(Mileage, 'travelDistance') THEN
    MESSAGE(FORMAT(Mileage));
    END;
    Kyle Hardin - ArcherPoint
Sign In or Register to comment.