3

Cinchoo ETL - Quick Start: Converting JSON to Yaml File

 2 years ago
source link: https://www.codeproject.com/Tips/5314773/Cinchoo-ETL-Quick-Start-Converting-JSON-to-Yaml-Fi
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1. Introduction

ChoETL is an open source ETL (extract, transform and load) framework for .NET. It is a code based library for extracting data from multiple sources, transforming, and loading into your very own data warehouse in .NET environment. You can have data in your data warehouse in no time.

This article talks about generating Yaml file from JSON format using Cinchoo ETL framework. It is very simple to use, with few lines of code, the conversion can be done. You can convert large files as the conversion process is stream based, quite fast and with low memory footprint.

2. Requirement

This framework library is written in C# using .NET 4.5 / .NET Core 3.x Framework.

3. How to Use

3.1 Sample Data

Let's begin by looking into a simple example of converting the below JSON input file.

Listing 3.1.1. Sample JSON Data Input File (swagger.json)
Copy Code
{
   "swagger":"2.0",
   "info":{
      "title":"UberAPI",
      "description":"MoveyourappforwardwiththeUberAPI",
      "version":"1.0.0"
   },
   "host":"api.uber.com",
   "schemes":[
      "https"
   ],
   "basePath":"/v1",
   "produces":[
      "application/json"
   ]
}

Expected yaml output would be.

Listing 3.1.2. Yaml Data Output File (swagger.yaml)
Copy Code
Swagger: 2.0
Info:
  Title: UberAPI
  Description: MoveyourappforwardwiththeUberAPI
  Version: 1.0.0
Host: api.uber.com
Schemes:
  - https
BasePath: /v1
Produces:
  - application/json

The first thing to do is to install ChoETL.Yaml nuget package. To do this, run the following command in the Package Manager Console.

.NET Framework / .NET Core

Copy Code
Install-Package ChoETL.Yaml

Now add ChoETL namespace to the program.

Copy Code
using ChoETL;

3.2 Quick Conversion

This approach shows how to convert JSON file to Yaml format with little piece of code. No setup / POCO class are needed.

Listing 3.2.1. Quick JSON to Yaml file conversion
JavaScript
Copy Code
private static void QuickConversion()
{       
    using (var r = new ChoJSONReader("swagger.json"))
    {
        using (var w = new ChoYamlWriter("swagger.yaml")
               .Configure(c => c.SingleDocument = true)
              )
        {
            w.Write(r);
        }
    }
}

Create an instance of ChoYamlWriter for producing Yaml (swagger.yaml) file. Then create an instance of ChoJSONReader object for reading swagger.json file. Voilà, Yaml file conversion happened with this little piece of code.

Sample fiddle: https://dotnetfiddle.net/rpxQVa

3.3 Using POCO Object

This approach shows you how to define POCO entity class and use them for the conversion process. This approach is more type safe and fine control over the conversion process like doing property validation, consuming callback machanism, etc.

First, create a class with properties matching JSON file

Listing 3.3.1. Mapping Class
JavaScript
Copy Code
public class SwaggerDocument
{
    public string Swagger { get; set; }
    public Info Info { get; set; }
    public string Host { get; set; }
    public List<string> Schemes { get; set; }
    public string BasePath { get; set; }
    public List<string> Produces { get; set; }
}

public class Info
{
    public string Title { get; set; }
    public string Description { get; set; }
    public string Version { get; set; }
}

Then use this class as below to do the conversion of the file.

Listing 3.3.2. Using POCO object to convert JSON to Yaml file
JavaScript
Copy Code
private static void UsingPOCO()
{
    using (var r = new ChoJSONReader<SwaggerDocument>("swagger.json"))
    {
        using (var w = new ChoYamlWriter<SwaggerDocument>("swagger.yaml")
               .Configure(c => c.SingleDocument = true)
              )
        {
            w.Write(r);
        }
    }
}

Sample fiddle: https://dotnetfiddle.net/HIX4CU

For more information about Cinchoo ETL, please visit the other CodeProject articles:

History

  • 8th October, 2021: Initial version

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK