0

I'm trying to fetch the values from the key/value pairs shown within the inner brackets inside the rows[]

The class I use to de-serialize the JSON object does not contain individual values given they may change between different data sets.

JSON Object looks like this:

{
  "columns": [
    "(Column 1)",
    "@Level",
    "@Exception",
    "@Properties"
  ],
  "rows": [
    [
      "2020-09-09T19:04:54.8422073Z",
      "Information",
      null,
      {
        "LogEventCategory": "System",
        "LogEventType": "Application Startup",
        "LogEventSource": "",
        "LogUserId": "",
        "LogUsername": "",
        "LogForename": "",
        "LogSurname": "",
        "LogData": "Application Starting Up",
        "MachineName": "DESKTOP-OS52032",
        "ProcessId": 16608,
        "ThreadId": 1,
        "ApplicationSource": "WebApp-RAZOR"
      }
    ],
    [
      "2020-09-09T19:09:06.5560521Z",
      "Information",
      null,
      {
        "LogEventCategory": "System",
        "LogEventType": "Application Startup",
        "LogEventSource": "",
        "LogUserId": "",
        "LogUsername": "",
        "LogForename": "",
        "LogSurname": "",
        "LogData": "Application Starting Up",
        "MachineName": "DESKTOP-OS52032",
        "ProcessId": 15588,
        "ThreadId": 1,
        "ApplicationSource": "WebApp-RAZOR"
      }
    ],
  "statistics": {
    "scannedEventCount": 11,
    "matchingEventCount": 6,
    "uncachedSegmentsScanned": false,
    "elapsedMilliseconds": 0.1267
  }
}

The values I'm trying to fetch are the ones listed:

"LogEventCategory": "System",
"LogEventType": "Application Startup",
"LogEventSource": "",
"LogUserId": "",
"LogUsername": "",
"LogForename": "",
"LogSurname": "",
"LogData": "Application Starting Up",
"MachineName": "DESKTOP-OS52032",
"ProcessId": 16608,
"ThreadId": 1,
"ApplicationSource": "WebApp-RAZOR"

When looping through the JSON object, I want to retrieve the values using some form of LINQ query or similar.

So far I've only been able to retrieve the first level items:

foreach (var item in logs.Rows)
{
    Timestamp = DateTime.Parse(item.First().ToString()), // = "2020-09-09T19:04:54.8422073Z"
    Level = item.Skip(1).First().ToString(), // = "Information"
    Exception = (item.Skip(2).First() ?? "-").ToString(), // = "-" in this example becuase json value is null
}

Class for de-serializing the JSON:

public class QueryLogEvents
{
    [JsonProperty("columns")]
    public string[] Columns { get; set; }

    [JsonProperty("rows")]
    public object[][] Rows { get; set; }

    [JsonProperty("statistics")]
    public Statistics Statistics { get; set; }
}

public class Statistics
{
    [JsonProperty("scannedEventCount")]
    public int ScannedEventCount { get; set; }

    [JsonProperty("matchingEventCount")]
    public int MatchingEventCount { get; set; }

    [JsonProperty("uncachedSegmentsScanned")]
    public bool UncachedSegmentsScanned { get; set; }

    [JsonProperty("elapsedMilliseconds")]
    public float ElapsedMilliseconds { get; set; }
}
8
  • Is the JSON data format under your control? Mixing types in an array is not ideal. Commented Sep 10, 2020 at 19:31
  • Nope the JSON array is returned from SEQ Logger. The values I've highlighted that I want to retrieve, may have additional key/value pairs between different log transactions, if that makes sense... Commented Sep 10, 2020 at 19:32
  • You might try modeling Rows as List<dynamic> and casting elements as needed, like stackoverflow.com/questions/29459064/… Commented Sep 10, 2020 at 19:35
  • The class I've shown in my question was created using "paste json as classes" in VS using the structure from the JSON, I just need to figure how to get values from the inner list of objects within the rows array. I don't understand the code in the suggested link you've shown. Commented Sep 10, 2020 at 19:42
  • What actual problem are you encountering? Is the 4th element not present? Your code doesn't show an attempt to access it. Commented Sep 10, 2020 at 20:03

1 Answer 1

0

OK, following some of the suggestions, through trial and error I got this to work using the dynamic JObject:

Note below 'result' is the JSON string returned from an API I was calling.

Please refer to the JSON structure shown in my original question to see how I got these properties:

dynamic jsonObject = JObject.Parse(result);
dynamic rows = jsonObject.rows;

foreach (var property in rows)
{   
   Console.WriteLine(property[0]); // = "2020-09-09T19:09:06.5560521Z"
   Console.WriteLine(property[1]); // = "Information"
   Console.WriteLine(property[2]); // = null
   Console.WriteLine(property[3]["LogEventCategory"]); // = "System"
   Console.WriteLine(property[3]["LogEventType"]); // = "Application Startup"
   Console.WriteLine(property[3]["LogEventSource"]);
   // And the rest of the properties required from the fourth object i.e. the items within the inner paranthesis.
   // Note in the foreach loop, we will iterate through 'rows' array
   // Instead of using console.writeline, in producton code i'll be adding ther items to a list.
}

Final Note: using the dynamic JObject parsing means we do no need any C# class at all now so the one shown in my question would be removed from my code.

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

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.