Understand the digital twins model parser

The Digital Twins Definition Language (DTDL) is described in the DTDL Specification V2. Users can use the Digital Twins Model Parser NuGet package to validate and query a DTDL model. The DTDL model may be defined in multiple files.

Install the DTDL model parser

The parser is available in NuGet.org with the ID: Microsoft.Azure.DigitalTwins.Parser. To install the parser, use any compatible NuGet package manager such as the one in Visual Studio or in the dotnet CLI.

dotnet add package Microsoft.Azure.DigitalTwins.Parser


At the time of writing, the parser version is 3.12.7.

Use the parser to validate a model

A model can be composed of one or more interfaces described in JSON files. You can use the parser to load all the files in a given folder and then validate all the files as a whole, including any references between the files:

  1. Create an IEnumerable<string> with a list of all model contents:

    using System.IO;
    string folder = @"c:\myModels\";
    string filespec = "*.json";
    List<string> modelJson = new List<string>();
    foreach (string filename in Directory.GetFiles(folder, filespec))
        using StreamReader modelReader = new StreamReader(filename);
  2. Instantiate the ModelParser and call ParseAsync:

    using Microsoft.Azure.DigitalTwins.Parser;
    ModelParser modelParser = new ModelParser();
    IReadOnlyDictionary<Dtmi, DTEntityInfo> parseResult = await modelParser.ParseAsync(modelJson);
  3. Check for validation errors. If the parser finds any errors, it throws an ParsingException with a list of errors:

        IReadOnlyDictionary<Dtmi, DTEntityInfo> parseResult = await modelParser.ParseAsync(modelJson);
    catch (ParsingException pex)
        foreach (var err in pex.Errors)
  4. Inspect the Model. If the validation succeeds, you can use the model parser API to inspect the model. The following code snippet shows how to iterate over all the models parsed and display the existing properties:

    foreach (var item in parseResult)

Next steps

The model parser API reviewed in this article enables many scenarios to automate or validate tasks that depend on DTDL models. For example, you could dynamically build a UI from the information in the model.