0

I'm getting a response from an api, but this response could be any type of json. I need to take the data and print it on a csv, is there anyway to convert that json to a dynamic object and iterate it or is there any other way to do it?.

My json response is:

{
"A":{
    "B":[
        {
            "C":"data"
            "E":"data"
            "G":{
                "H":[
                    {"I":"data"},
                    {...},
                    {...}
                ]
            }
        },

        {...},

        {...},

        {...}
    ]
}

}

and latter it could be

{
"A":{
    "B":[
        {
            "C":"data"
            "E":"data"
            "G":{
                "H":[
                    {"I":"data"},
                    {...},
                    {...},
                    {"K":[
                        {"w":"data"},
                        {...},
                        {...},
                        {"d":[
                            {"d":"data"},
                            {...},
                            {"d":[
                                {"h","data"},
                                {...},
                                {...}
                            ]}
                        ]}
                    ]}
                ]
            }
        },

        {...},

        {...},

        {...}
    ]
}

}

Silver Qui.
  • 49
  • 2
  • 8
  • Possible duplicate of [Deserialize JSON into C# dynamic object?](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) – Tom W Feb 18 '19 at 15:39
  • 2
    It's not at all clear what the difference between your two structures is, other than the second having more nodes defined. You say you want to convert your JSON to CSV, but the output you're showing is not CSV. Please [edit] your question to clarify what you are looking to do. Please also show what you've tried so far. – Heretic Monkey Feb 18 '19 at 15:41
  • ... Lookup JObject – Florian Schmidinger Feb 18 '19 at 15:41
  • 1
    JSON parsers are "dynamic" by definition. They can read any JSON document. If you use `JObject.Parse` on that JSON string you'd get a JObject back and access its properties by name. What you posted though doesn't look like something that could be represented as a CSV. How do you differentiate rows and columns here? – Panagiotis Kanavos Feb 18 '19 at 15:43
  • Perhaps (?), what you're asking is how to recursivelly flatten the JSON payload into name/value pairs? And each object is written in a separate line? – Panagiotis Kanavos Feb 18 '19 at 15:45
  • no Im telling that it could be a dynamic json, I don't know what is going to be then I need to take that response, Iterate through objects and array an print all the data like: data,data,data,data,data,data – Silver Qui. Feb 18 '19 at 15:55

1 Answers1

0

With Cinchoo ETL - an open source library available to parse json and convert them to CSV

string json = @"
{
    ""name"":""John"",
    ""age"":30,
    ""cars"": {
    ""car1"":""Ford"",
    ""car2"":""BMW"",
    ""car3"":""Fiat"",
    ""Country"": [
        ""USA"",
        ""Mexico""
        ]
    }
}";

StringBuilder csv = new StringBuilder();
using (var p = ChoJSONReader.LoadText(json)
    .WithJSONPath("$")
    )
{
    using (var w = new ChoCSVWriter(csv)
        .WithFirstLineHeader()
        )
    {
        w.Write(p);
    }
}

Console.WriteLine(csv.ToString());

Output:

name,age,cars_car1,cars_car2,cars_car3,cars_Country_0,cars_Country_1
John,30,Ford,BMW,Fiat,USA,Mexico

Disclaimer: I'm the author of this library.

Cinchoo
  • 6,088
  • 2
  • 19
  • 34