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());
        }
2
  • 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? Commented May 13, 2019 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 Commented May 13, 2019 at 22:38

2 Answers 2

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.

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

1 Comment

Thanks heyNow. Updated the code to make it work for me: 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);
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());
            }

3 Comments

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[*]))
I'm not able to reproduce error. Edit your question and add your code there.
@Cinchoo as discussed previously you should disclose your relationship to the library when promoting your own code as a solution to others.

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.