Create JSON file using C/AL Code

navuser1navuser1 Posts: 1,315Member
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 Posts: 95Member
    Either implement your own, or backport Codeunit 5459 JSON Management from newer NAV and use functions there to generate your JSON
    Arturas Kulsinskas
    NaviLogic
  • kylehardinkylehardin Posts: 179Member
    There are also several useful functions on Table 1236 - backport that too
    Kyle Hardin - ArcherPoint
  • navuser1navuser1 Posts: 1,315Member
    edited 2020-02-11
    I will check
    Now or Never
  • navuser1navuser1 Posts: 1,315Member
    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 Posts: 179Member
    I think that was introduced in NAV 2017. 2018 for sure.
    Kyle Hardin - ArcherPoint
  • navuser1navuser1 Posts: 1,315Member
    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 Posts: 1,315Member
    edited 2020-02-12
    code for iteration(Loop) to create a JSON file ?
    Now or Never
  • kylehardinkylehardin Posts: 179Member
    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 [email protected]();
    VAR
    [email protected] : Record 99008535;
    [email protected] : TEMPORARY Record 1236;
    [email protected] : Codeunit 1297;
    [email protected] : OutStream;
    [email protected] : InStream;
    [email protected] : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpStatusCode";
    [email protected] : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.Specialized.NameValueCollection";
    [email protected] : Text;
    [email protected] : Text;
    [email protected] : 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.