Hope this might be helpful:
In AL for Business Central, when creating JSON, special characters are generally handled automatically by the JsonToken
and JsonBuilder
(or JsonObject
) data types, as they perform proper escaping according to JSON standards.
You don't typically need to manually escape characters like double quotes ("
), backslashes (\
), newlines (\n
), tabs (\t
), or control characters. The WriteValue
methods of JsonBuilder
or assigning values to JsonObject
members will take care of this for you.
local procedure CreateJsonWithSpecialChars()
var
JsonBuilder: JsonBuilder;
JsonText: Text;
begin
JsonBuilder.WriteStartObject(); // {
// String with double quotes, newlines, tabs, and backslashes
JsonBuilder.WriteName('Description');
JsonBuilder.WriteValue('This is a "test" string with\nnewlines and\ttabs and a \\ backslash.');
// String with characters that might seem special but are fine directly
JsonBuilder.WriteName('ProductCode');
JsonBuilder.WriteValue('PROD-BC/123-ÄÖÜ');
// Unicode characters are also handled
JsonBuilder.WriteName('UnicodeExample');
JsonBuilder.WriteValue('你好世界 – नमस्ते दुनिया');
JsonBuilder.WriteEndObject(); // }
JsonBuilder.WriteTo(JsonText);
Message('%1', JsonText);
// Expected output will have special characters escaped:
// {"Description":"This is a \"test\" string with\nnewlines and\ttabs and a \\\\ backslash.","ProductCode":"PROD-BC\/123-ÄÖÜ","UnicodeExample":"你好世界 – नमस्ते दुनिया"}
end;
Example using JsonObject
(for more structured data or if you have existing objects):
local procedure CreateJsonWithSpecialCharsUsingObject()
var
JsonObject: JsonObject;
JsonToken: JsonToken;
JsonText: Text;
begin
JsonObject.Add('Description', 'This is another "test" with\nnewlines and\ttabs.');
JsonObject.Add('ItemName', 'Item with & symbols and < > brackets.'); // & is not special in JSON string values
JsonObject.Add('Path', 'C:\\Program Files\\App'); // Backslashes will be escaped
JsonObject.WriteTo(JsonText);
Message('%1', JsonText);
// Expected output:
// {"Description":"This is another \"test\" with\nnewlines and\ttabs.","ItemName":"Item with & symbols and < > brackets.","Path":"C:\\\\Program Files\\\\App"}
end;
When using Business Central's built-in JsonToken
, JsonBuilder
, or JsonObject
types to construct JSON in AL, you generally do not need to manually escape special characters within string values. The AL runtime and JSON serialization logic handle the necessary escaping automatically to produce valid JSON.
✅ Mark this answer as verified if it helps you.