Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#
Tip/Trick

Cinchoo ETL - Quick Start: Converting JSON to YAML File

Rate me:
Please Sign up or sign in to vote.
4.60/5 (2 votes)
24 Nov 2021CPOL2 min read 7.9K   4   2
Quick tutorial about converting JSON to YAML file using Cinchoo ETL
In this tip, you will learn how to convert JSON to YAML quickly 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.

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)
JavaScript
{
   "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)
YAML
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

Install-Package ChoETL.Yaml

Now add ChoETL namespace to the program.

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
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 mechanism, etc.

First, create a class with properties matching JSON file.

Listing 3.3.1. Mapping Class
JavaScript
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
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA24-Nov-21 4:40
professionalȘtefan-Mihai MOGA24-Nov-21 4:40 
GeneralRe: My vote of 5 Pin
Cinchoo24-Nov-21 5:33
Cinchoo24-Nov-21 5:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.