0

I created an ASP.NET Application, where I have to parse a csv file with a json structure. The csv file itself is structured like:

{"Instance":"abc","Date":"2019-06-03T00:00:02.056Z","Identification":"someFunction","Type":"DurationInMs","Value":"5","iserror":"False""}

I get the jsonCsvData as a string and tried to parse it. Then I want to save some of the elements of this json object into a db.

        public IActionResult ReadJsonCsvData(string jsonCsvData)
        {
            Console.WriteLine("ReadJsonCsvData");

            ChartData chartData = new ChartData();

            var lines = jsonCsvData.Split("\n");

            foreach (var line in lines)
            {
                var values = JObject.Parse(line);

                var first = string.Concat(values["Instance"]); //Output for first: ""
            }
        }

The problem now is, that the variable first is an empty string. The result should be (like in the json structure example above) "abc".

Thank you in advance!

peeyush singh
  • 1,337
  • 1
  • 12
  • 23
  • Just do something like `JsonConvert.DeserializeObject(jsonCsvData)` to get you going, then you can start creating concrete classes that match your schema. – Vidmantas Blazevicius Jul 24 '19 at 07:59
  • @VidmantasBlazevicius: There's no need to do that. This code should work. – Jon Skeet Jul 24 '19 at 07:59
  • That doesn't look like CSV, that looks like line-delimited JSON. To parse such a file see [Line delimited json serializing and de-serializing](https://stackoverflow.com/a/29729419). – dbc Jul 24 '19 at 07:59
  • You should put a debug point and check what are values in your jsonCsvData and values variable – peeyush singh Jul 24 '19 at 08:00
  • The end of your JSON doesn't look valid to me... I strongly suspect you're not parsing the exact data you think you are. I suggest you dump the value of `line` to the console on each iteration. – Jon Skeet Jul 24 '19 at 08:00
  • There is an extra quote at the end of the JSON, which makes it invalid. Other than that, doing `first.Value()` should give you `"abc"`. – Sweeper Jul 24 '19 at 08:02
  • You can take advantage of VS's Paste Special Feature to create a class from Json or XML. Then It would be very easy to deserialize using JsonConvert.DeserializeObject(jsonCsvData). – Badhon Jain Jul 24 '19 at 08:13

1 Answers1

0

I don't know if it will help but here is my solution (remove one of " at the end of your Json).

I use the "Jobject" to parse Json as I want. Import this two reference.

using Newtonsoft.Json.Linq;
using Newtonsoft;

Then you have to create your JObject :

JObject o = JObject.Parse(myJsonString);

Then to retrieve specifics data, you just have to search in your object just like you do with a dictionary with key :

instanceFromJson = o["Instance"].ToString;
dateFromJson = o["Date"].ToString;

If you have a table in your "instance" json object you can retrieve all data from this list just like that :

foreach (var item in o["Instance"]["tabFromInstanceObject"])
{
    MyList.Add(item);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459