Cinchoo ETL json to csv

196 Views Asked by At

I am trying to format a json to csv file.

here is the the data struct

public class Location
{
    [JsonProperty("latitude")]
    public double Latitude { get; set; }

    [JsonProperty("longitude")]
    public double Longitude { get; set; }
}

public class Monitor
{
    [JsonProperty("channelId")]
    public int ChannelId { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("alias")]
    public string Alias { get; set; }

    [JsonProperty("active")]
    public bool Active { get; set; }

    [JsonProperty("typeId")]
    public int TypeId { get; set; }

    [JsonProperty("pollutantId")]
    public int PollutantId { get; set; }

    [JsonProperty("units")]
    public string Units { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }
}

public class StationCall
{
    [JsonProperty("stationId")]
    public int StationId { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("shortName")]
    public string ShortName { get; set; }

    [JsonProperty("stationsTag")]
    public string StationsTag { get; set; }

    [JsonProperty("location")]
    public Location Location { get; set; }

    [JsonProperty("timebase")]
    public int Timebase { get; set; }

    [JsonProperty("active")]
    public bool Active { get; set; }

    [JsonProperty("owner")]
    public string Owner { get; set; }

    [JsonProperty("regionId")]
    public int RegionId { get; set; }

    [JsonProperty("monitors")]
    public Monitor[] Monitor { get; set; }

    [JsonProperty("StationTarget")]
    public string StationTarget { get; set; }

    [JsonProperty("additionalTimebases")]
    public string AdditionalTimebases { get; set; }

    [JsonProperty("isNonContinuous")]
    public string IsNonContinuous { get; set; }

    public static string fileName = "stations.txt";
  
   
}

the problem is in the csv result, the class Monitor doesn't format correctly

StationId,Name,ShortName,StationsTag,Location.Latitude,Location.Longitude,Timebase,Active,Owner,RegionId,Monitor,StationTarget,AdditionalTimebases,IsNonContinuous 63,משושים-דרדרה,Meshushim,Meshushim,00.0000,00.3583,60,True,None,16,"Sfika_App.Entities.Monitor,Sfika_App.Entities.Monitor,Sfika_App.Entities.Monitor",None,,

using the package:

 using (var parser = new ChoCSVWriter<StationCall>("D:/Export.csv").WithFirstLineHeader().UseNestedKeyFormat(true))
        {
            //parser.with
            parser.Write(jsonContent);
        }

what am i doing wrong?

1

There are 1 best solutions below

1
On BEST ANSWER

You will need to decorate the Monitor property with RangeAttribute to specify the possible expected array range

public class StationCall
{
    [JsonProperty("stationId")]
    public int StationId { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("shortName")]
    public string ShortName { get; set; }

    [JsonProperty("stationsTag")]
    public string StationsTag { get; set; }

    [JsonProperty("location")]
    public Location Location { get; set; }

    [JsonProperty("timebase")]
    public int Timebase { get; set; }

    [JsonProperty("active")]
    public bool Active { get; set; }

    [JsonProperty("owner")]
    public string Owner { get; set; }

    [JsonProperty("regionId")]
    public int RegionId { get; set; }

    [JsonProperty("monitors")]
    [Range(0, 1)]
    public Monitor[] Monitor { get; set; }

    [JsonProperty("StationTarget")]
    public string StationTarget { get; set; }

    [JsonProperty("additionalTimebases")]
    public string AdditionalTimebases { get; set; }

    [JsonProperty("isNonContinuous")]
    public string IsNonContinuous { get; set; }

    public static string fileName = "stations.txt";
  
   
}