Transform JSON payloads in AL
JSON is the payload format used by many REST services. AL JSON types let you create an outbound document, parse an inbound document, and validate that the document matches the expected integration contract.
Map JSON structures to AL types
Choose an AL type based on the JSON value that you need to represent.
| JSON structure | AL type | Example |
|---|---|---|
| Object | JsonObject |
{ "orderNo": "SO-1001" } |
| Array | JsonArray |
[1, 2, 3] |
| Any JSON node | JsonToken |
An object, array, value, or null node. |
| Scalar value | JsonValue |
Text, number, Boolean, date, or null. |
The JSON types are reference types. Assigning one JSON variable to another can refer to the same JSON tree. Use Clone when you need an independent copy.
JsonObject stores named properties. JsonArray stores values at zero-based indexes. JsonToken is useful when the structure isn’t known until runtime or when you need to inspect a value before converting it.
JSON has no native date primitive. Date values are represented as strings in the service's agreed format.
Create and serialize a request body
Build an outbound body with JsonObject.Add, then serialize it with WriteTo.
local procedure BuildFulfillmentRequest(
OrderNo: Code[20]; Description: Text[100]; Quantity: Decimal) JsonText: Text
var
RequestBody: JsonObject;
Lines: JsonArray;
Line: JsonObject;
begin
RequestBody.Add('orderNo', OrderNo);
Line.Add('description', Description);
Line.Add('quantity', Quantity);
Lines.Add(Line);
RequestBody.Add('lines', Lines);
RequestBody.WriteTo(JsonText);
end;
Using JSON types avoids manual quoting and escaping. A decimal remains a JSON number, a Boolean remains a JSON Boolean, and text is escaped during serialization. After serialization, write the text to HttpContent and set the content type to application/json.
Keep payload construction separate from HTTP communication. A serializer procedure can be tested with known input and expected JSON without calling an external service.
Parse and navigate a response
Use ReadFrom to parse text. Capture its Boolean result so malformed JSON becomes a controlled contract error.
local procedure ReadExternalOrderId(ResponseText: Text) ExternalOrderId: Integer
var
ResponseObject: JsonObject;
IdToken: JsonToken;
begin
if not ResponseObject.ReadFrom(ResponseText) then
Error('The service response isn''t valid JSON. Response: %1',
CopyStr(ResponseText, 1, 250));
if not ResponseObject.Get('id', IdToken) then
Error('The service response doesn''t contain the required id property. Response: %1',
CopyStr(ResponseText, 1, 250));
if not IdToken.IsValue() then
Error('The id property isn''t a JSON value.');
ExternalOrderId := IdToken.AsValue().AsInteger();
end;
IsValue establishes that a token is a scalar JSON value, not that it is an integer. AsInteger still raises a runtime conversion error for null, a nonnumeric or noninteger value, or a number outside the AL Integer range. This fragment doesn't provide a custom diagnostic for every primitive-type conversion failure.
JsonObject also provides typed getters, such as GetText, GetInteger, GetObject, and GetArray. Typed getters reduce conversion code when the runtime version and contract guarantee the property. Get with JsonToken gives you more control over the message when a property is missing or has an unexpected structure.
For nested values, retrieve and validate each boundary. The following example reads the first line from a response with a lines array.
local procedure ReadFirstLineDescription(ResponseObject: JsonObject) Description: Text
var
LinesToken: JsonToken;
LineToken: JsonToken;
DescriptionToken: JsonToken;
Lines: JsonArray;
Line: JsonObject;
begin
if not ResponseObject.Get('lines', LinesToken) or not LinesToken.IsArray() then
Error('The response must contain a lines array.');
Lines := LinesToken.AsArray();
if Lines.Count() = 0 then
Error('The response lines array must contain at least one item.');
Lines.Get(0, LineToken);
if not LineToken.IsObject() then
Error('The first lines entry must be an object.');
Line := LineToken.AsObject();
if not Line.Get('description', DescriptionToken) or not DescriptionToken.IsValue() then
Error('The first line must contain a description value.');
Description := DescriptionToken.AsValue().AsText();
end;
Likewise, checking IsValue for a description doesn't by itself prove the service supplied the required text type. Apply the service's exact primitive-type and business-rule checks when extending this teaching pattern.
Validate the payload contract
Valid JSON can still be unusable. Validate the properties that the business operation requires before you change Business Central data.
Check the following contract details:
The root is the expected object or array.
Required properties exist.
Each property has the expected JSON type.
Arrays contain the expected element type and acceptable number of items.
Identifiers, quantities, dates, and enum-like text values meet business rules.
Optional properties have an explicit default or absence policy.
Avoid replacing a missing required value with an empty string. That approach converts an integration defect into incomplete business data. Report the property name and preserve a safe excerpt of the response so that a developer can compare the payload with the agreed contract.
Retain only response details that your organization's redaction and retention policy permits. A truncated raw response isn't automatically safe to show or log.
See JsonObject data type for the current JSON object API.