1

New to C# and hopefully there is a simple fix for this. Using Newtonsoft and CsvHelper packages to convert a json to csv file. I dont have any control over input json. so, input json could have 'attr4' 'attr5' and so on (or without 'attr1' .. ) . in the code below, section using foreach is working as expected.

I am hoping to avoid foreach in the second section (mycsv2.csv writing), but thats not working.

I am new to all these and I am hoping this is simple mistake.

    public static void jsonToCsv()
    {
        string json = @"{
                'attr1': 'val1',
                'attr2': 'val2',
                'attr3': 'val3'                      
            }";

        //working
        var records = new List<object> {};
        JObject jObj = JObject.Parse(json);
        dynamic obj = new ExpandoObject();

        foreach (JProperty property in jObj.Properties())
        {
            AddProperty(obj, property.Name, property.Value.ToString());
        }
        records.Add(obj);                    

        using (var writer = new StreamWriter("C:\\csv\\mycsv1.csv"))
        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
        {
            csv.WriteRecords(records);
        }

        //NOT working

        var recordsD = new List<object> {};
        JObject jObjD = JObject.Parse(json);
        dynamic objD = new ExpandoObject();
        objD = jObjD.ToObject<object>();            
        records.Add(objD);

        using (var writer = new StreamWriter("C:\\csv\\mycsv2.csv"))
        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
        {
            csv.WriteRecords(recordsD);
        }

    }

    public static void AddProperty(ExpandoObject expando, string propertyName, object propertyValue)
    {
        // ExpandoObject supports IDictionary so we can extend it like this
        var expandoDict = expando as IDictionary<string, object>;
        if (expandoDict.ContainsKey(propertyName))
            expandoDict[propertyName] = propertyValue;
        else
            expandoDict.Add(propertyName, propertyValue);
    }

2 Answers 2

2

You could cast to ExpandoObject not Object in the second query, like the following code:

var recordsD = new List<object> { };
JObject jObjD = JObject.Parse(json);
dynamic objD = new ExpandoObject();

objD = jObjD.ToObject<ExpandoObject>();
recordsD.Add(objD);

This give the same result as first request.

I hope this help you fix the issue.

Sign up to request clarification or add additional context in comments.

Comments

0

Alternatively you can use Cinchoo ETL to do the conversion of JSON to CSV as below (no configuration / schema needs to be defined to parse variable files)

Install nuget package

Install-Package ChoETL.JSON

string json = @"{
    'attr1': 'val1',
    'attr2': 'val2',
    'attr3': 'val3'                      
}";

StringBuilder csv = new StringBuilder();
using (var r = ChoJSONReader.LoadText(json))
{
    using (var w = new ChoCSVWriter(csv)
        .WithFirstLineHeader()
        )
        w.Write(r);
}

Console.WriteLine(csv.ToString());

Output:

attr1,attr2,attr3
val1,val2,val3

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.