0

I am trying to convert a json string, containing array elements, to a .csv file. Below is the json string format:

{"inputFile": [["Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10"], ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"], ["K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"]]}

solved. Thanks heyNow.

            dynamic dynObj = JsonConvert.DeserializeObject(json);
            var rowElements = new List<string>();

            foreach (var data in dynObj.inputFile)
            {
                var row = new List<string>();
                foreach (var dataItem in data)
                {
                    var item = Convert.ToString(dataItem);
                    row.Add(item);
                }

                rowElements.Add( String.Join(",", row)+"\n");
            }
            var CSV = String.Join("",rowElements);
            Console.WriteLine(CSV);

For RaJN:
Updated code to replace json file path to json string:

        StringBuilder msg = new StringBuilder();
        using (var p = ChoJSONReader.LoadText(jsonString)
            .WithJSONPath("$.inputFile[*]")
            )
        {
            using (var w = new ChoCSVWriter(msg))
            {
                w.Write(p);
            }
            Console.WriteLine(msg.ToString());
        }
swhat
  • 37
  • 1
  • 5
  • 6
    First telling us you had no luck doesn't mean anything to anyone, secondly deserialising a json string is one of the most commonly asked question on stackoverflow for C#, what did you research and why didn't it work? – TheGeneral May 13 '19 at 21:56
  • You say: "With no luck".... That doesn't help us. What error do you get!!! We can't help you this way – A. Vreeswijk May 13 '19 at 22:38

2 Answers2

4

try with newtonsoft jsonparser.
If you can get your JSON as a string..

dynamic dynObj = JsonConvert.DeserializeObject(jsonString);

string CSV = "";

foreach (var data in dynObj.inputFile)
{
    List<string> row = new List<string>();

    foreach(var dataItem in data)
    {
        row.Add(dataItem);
    }

    CSV+=string.Join(row, ",");
}

You will get 1 giant string containing all the values as a CSV. Let us know if this is what you want.

heyNow
  • 866
  • 2
  • 19
  • 42
  • 1
    Thanks heyNow. Updated the code to make it work for me: `dynamic dynObj = JsonConvert.DeserializeObject(json); var rowElements = new List(); foreach (var data in dynObj.inputFile) { var row = new List(); foreach (var dataItem in data) { var item = Convert.ToString(dataItem); row.Add(item); } rowElements.Add( String.Join(",", row)+"\n"); } var CSV = String.Join("",rowElements); ` – swhat May 14 '19 at 01:57
0

Here is how you can generate CSV from your JSON file using Cinchoo ETL

        StringBuilder msg = new StringBuilder();
        using (var p = new ChoJSONReader("*** YOUR JSON FILE PATH ***")
            .WithJSONPath("$.inputFile[*]")
            )
        {
            using (var w = new ChoCSVWriter(msg))
            {
                w.Write(p);
            }
            Console.WriteLine(msg.ToString());
        }

Output:

Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8,Column9,Column10
A,B,C,D,E,F,G,H,I,J
K,L,M,N,O,P,Q,R,S,T

UPDATE:

            string json = @"
{
  ""inputFile"": [
    [""Column1"", ""Column2"", ""Column3"", ""Column4"", ""Column5"", ""Column6"", ""Column7"", ""Column8"", ""Column9"", ""Column10""],
    [ ""A"", ""B"", ""C"", ""D"", ""E"", ""F"", ""G"", ""H"", ""I"", ""J"" ],
    [ ""K"", ""L"", ""M"", ""N"", ""O"", ""P"", ""Q"", ""R"", ""S"", ""T"" ]
  ]
}";

            StringBuilder msg = new StringBuilder();
            using (var p = ChoJSONReader.LoadText(json)
                .WithJSONPath("$.inputFile[*]")
                )
            {
                using (var w = new ChoCSVWriter(msg))
                {
                    w.Write(p);
                }
                Console.WriteLine(msg.ToString());
            }
Cinchoo
  • 6,088
  • 2
  • 19
  • 34
  • I tried it again, but it was giving me a `Failed to assign 'System.Object[]' fallback value to 'Value' field.` One thing I changed from your code was instead of passing in a json file path, i wanted to pass in a json string. `using(var p = ChoJsonReader.LoadText(jsonString).WithJSONPath("$.inputFile[*]))` – swhat May 15 '19 at 14:14
  • I'm not able to reproduce error. Edit your question and add your code there. – Cinchoo May 15 '19 at 19:42
  • @Cinchoo as [discussed previously](https://stackoverflow.com/a/44889944/2325377) you should disclose your relationship to the library when promoting your own code as a solution to others. – DVS Nov 16 '20 at 17:20