Dynamics 365 Business Central: YAML to JSON and JSON to YAML conversion.

When talking about data exchange between systems, probably the most common ways to represent (serialize) objects are by using JSON and XML.

JSON (JavaScript Object Notation) in particular is the de-facto standard protocol in the REST-ful world. It is a lightweight, text-based data interchange format that is both human-readable and machine-readable. It was derived from JavaScript but is language-independent, making it a popular choice for data exchange between different systems and applications.

YAML (YAML Ain’t Markup Language, originally “Yet Another Markup Language“) is a human-friendly data serialization standard that’s commonly used for configuration files and data exchange. It’s designed to be more readable than JSON and XML, with a focus on being human-readable and human-writable.

YAML and JSON are both data serialization formats.

While both JSON and YAML are used for data representation, they have some key differences. JSON is a more rigid format ideal for data interchange, while YAML is more human-readable and better suited for configuration files.

YAML has some advantages compared to JSON for some scenarios:

  • Readability: YAML’s clean syntax makes data more human-readable compared to JSON, facilitating easier comprehension and editing, especially for complex configurations or nested structures.
  • Size: YAML often requires fewer characters to represent the same data, reducing file size and improving efficiency.
  • Flexibility: YAML supports additional data types like dates and binary, enhancing versatility in representing diverse information.
  • Comments: YAML allows comments, aiding documentation and providing context for different sections or entries within the data.

In some scenarios, a JSON to YAML conversion (or vice versa) enables compatibility between systems or tools that prefer one format over the other, promoting a seamless data exchange.

To start supporting this interoperability, in the latest AL language extension 14.3 the JsonObject AL object now has two new methods:

  • procedure ReadFromYaml(String: Text): Boolean: reads the YAML data from the string into a JsonObject variable.
  • procedure ReadFromYaml(InStream: InStream): Boolean: reads the YAML data from the InStream into a JsonObject variable.
  • procedure WriteToYaml(var String: Text): Boolean: serializes and writes the JsonObject as YAML to a given Text object.
  • procedure WriteToYaml(OutStream: OutStream): Boolean: serializes and writes the JsonObject as YAML to a given OutStream object.

Here is an example of usage. Imagine to have a JSON representation of an object and I need to create a YAML representation of the same object:

For this example, I’m loading (using an InStream) the following JSON file in AL:

{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
}
]
}

and here is the generated YAML representation of the object:

firstName: John
lastName: Smith
isAlive: true
age: 25
address:
streetAddress: 21 2nd Street
city: New York
state: NY
postalCode: 10021-3100
phoneNumbers:
- type: home
number: 212 555-1234

The same works in the opposite way.

Imagine that I neet to import in Dynamics 365 Business Central the following YAML file, containing the definition of a pipeline for a .NET application (this is just a YAML object):

To read the YAML file and convert it to JSON, you can use the following AL code:

and here is the JSON object created from the YAML definition:

This is a quite hidden addition that can help you on working with these protocols.

Original Post https://demiliani.com/2025/03/10/dynamics-365-business-central-yaml-to-json-and-json-to-yaml-conversion/

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Leave a reply

Join Us
  • X Network2.1K
  • LinkedIn3.8k
  • Bluesky0.5K
Support The Site
Events
March 2025
MTWTFSS
      1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31       
« Feb   Apr »
Follow
Sign In/Sign Up Sidebar Search
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...