Create a JSON with AL-Business Central

  local procedure CreateJSON(Customer: Record Customer)

var ShiptoAddress: Record "Ship-to Address"; JArray: JsonArray; CustObject: JsonObject; JsonObject: JsonObject; ShiptoObject: JsonObject; JsonData: Text; begin Clear(JsonObject); Clear(CustObject); CustObject.Add('No', Customer."No."); CustObject.Add('Address', Customer.Address); CustObject.Add('Address_2', Customer."Address 2"); CustObject.Add('City', Customer.City); CustObject.Add('County', Customer.County); CustObject.Add('Country_Region', Customer."Country/Region Code"); CustObject.Add('Post_Code', Customer."Post Code"); ShiptoAddress.SetRange("Customer No.", Customer."No."); if ShiptoAddress.FindSet() then begin Clear(JArray); repeat Clear(ShiptoObject); ShiptoObject.Add('Code', ShiptoAddress.Code); ShiptoObject.Add('Address', ShiptoAddress.Address); ShiptoObject.Add('Address_2', ShiptoAddress."Address 2"); ShiptoObject.Add('City', ShiptoAddress.City); ShiptoObject.Add('County', ShiptoAddress.County); ShiptoObject.Add('Post_Code', ShiptoAddress."Post Code"); JArray.Add(ShiptoObject); until ShiptoAddress.Next() = 0; CustObject.Add('Ship-to', JArray); end; JsonObject.Add('Customer', CustObject); JsonObject.WriteTo(JsonData); end;

Sample Json

{ "Customer": { "No": "10000", "Address": "192 Market Square", "Address_2": "", "City": "", "County": "NJ", "Country_Region": "US", "Post_Code": "", "Ship-to": [ { "Code": "LEWES ROAD", "Address": "2 Lewes Road", "Address_2": "", "City": "Atlanta", "County": "GA", "Post_Code": "31772" }, { "Code": "PARK ROAD", "Address": "10 Park Road", "Address_2": "", "City": "Atlanta", "County": "GA", "Post_Code": "31772" } ] } }

No comments:

Post a Comment