Skip to main content

Does anyone have an example on how to parse the following response using C#?
I was able to just Json
My end goal is to get the json string into a list to create a file with.

Thank you for any assistance!

{
  "activity_logs": [
    {
      "data": "{\\"board_id\\":2165785546,\\"group_id\\":\\"new_group2051\\",\\"is_top_group\\":false,\\"pulse_id\\":2165787062,\\"pulse_name\\":\\"Tyler\\",\\"column_id\\":\\"email_address\\",\\"column_type\\":\\"email\\",\\"column_title\\":\\"Email Address\\",\\"value\\":{\\"column_settings\\":{\\"includePulseInSubject\\":true,\\"ccPulse\\":true,\\"bccList\\":\\"\\"}},\\"previous_value\\":{\\"email\\":\\"tyler@email.com\\",\\"text\\":\\"tyler@email.com\\",\\"changed_at\\":\\"2022-02-15T21:18:48.297Z\\",\\"column_settings\\":{\\"includePulseInSubject\\":true,\\"ccPulse\\":true,\\"bccList\\":\\"\\"}},\\"is_column_with_hide_permissions\\":false,\\"previous_textual_value\\":\\"tyler@email.com\\"}"
    },
    {
      "data": "{\\"board_id\\":2165785546,\\"group_id\\":\\"new_group2051\\",\\"is_top_group\\":false,\\"pulse_id\\":216578711,\\"pulse_name\\":\\"Nicholas \\",\\"column_id\\":\\"email_address\\",\\"column_type\\":\\"email\\",\\"column_title\\":\\"Email Address\\",\\"value\\":{\\"column_settings\\":{\\"includePulseInSubject\\":true,\\"ccPulse\\":true,\\"bccList\\":\\"\\"}},\\"previous_value\\":{\\"email\\":\\"nicholas@email.com\\",\\"text\\":\\"nicholas@email.com\\",\\"changed_at\\":\\"2022-02-16T04:44:52.046Z\\",\\"column_settings\\":{\\"includePulseInSubject\\":true,\\"ccPulse\\":true,\\"bccList\\":\\"\\"}},\\"is_column_with_hide_permissions\\":false,\\"previous_textual_value\\":\\"nicholas@email.com\\"}"
    },
    {
      "data": "{\\"board_id\\":2165785546,\\"group_id\\":\\"new_group2051\\",\\"is_top_group\\":false,\\"pulse_id\\":216578711,\\"pulse_name\\":\\"Nicholas \\",\\"column_id\\":\\"batch_5\\",\\"column_type\\":\\"text\\",\\"column_title\\":\\"Batch #\\",\\"value\\":{\\"value\\":\\"75\\"},\\"previous_value\\":{\\"value\\":\\"74\\"},\\"is_column_with_hide_permissions\\":false}"
    },
    {
      "data": "{\\"board_id\\":2165785546,\\"group_id\\":\\"new_group2051\\",\\"pulse_id\\":216578711,\\"is_top_group\\":false,\\"value\\":{\\"name\\":\\"Nicholas \\"},\\"previous_value\\":{\\"name\\":\\"Nicholas \\"},\\"column_type\\":\\"name\\",\\"column_title\\":\\"Name\\"}"
    }
  ]
}

using
JsonConvert.DeserializeObject on the values comes back with the error:
$exception {“The best overloaded method match for ‘Newtonsoft.Json.JsonConvert.DeserializeObject(string)’ has some invalid arguments”} Microsoft.CSharp.RuntimeBinder.RuntimeBinderException

Here is my ugly code. I’ve tried deserializing it into a class but it came back with errors. The class was hard to make with IContainers in IContainers.

var responseData = JObject.Parse(responseText).SelectToken("data").SelectToken("boards").SelectToken("activity_logs");
dynamic updatedRecords = JsonConvert.DeserializeObject(responseData.ToString());

foreach (var record in updatedRecords)
{
List<Dictionary<string, string>> records = new List<Dictionary<string, string>>();
Dictionary<string, string> fields = new Dictionary<string, string>();
dynamic updates = JsonConvert.DeserializeObject(JObject.Parse(record.ToString()).SelectToken("data").ToString());
foreach(var update in updates)
{
    switch (update.Name.ToString())
    {
        case "column_id":
            fields.Add(update.Name.ToString(), update.Value.ToString());
            break;
        case "pulse_name":
            fields.Add(update.Name.ToString(), update.Value.ToString());
            break;
        case "value":
            dynamic values = JsonConvert.DeserializeObject(JObject.Parse(update.Value.ToString()));
            if (update.Name.ToString().Contains("column_settings"))
            {
                foreach (var value in values)
                {
                    dynamic columns = JsonConvert.DeserializeObject(JObject.Parse(value.Value.ToString()));
                    foreach(var column in columns)
                    {
                        fields.Add($"Value_{column.Name.ToString()}", column.Value.ToString());
                    }
                                                
                }
            }
            else
            {
                foreach (var value in values)
                {
                    fields.Add($"Value_{value.Name.ToString()}", value.Value.ToString());
                }
            }
                                        
            break;
        case "previous_value":
            dynamic prevValues = JsonConvert.DeserializeObject(JObject.Parse(update.Value.ToString()));
            foreach (var prevalue in prevValues)
            {
                fields.Add($"Prevalue_{prevalue.Name.ToString()}", prevalue.Value.ToString());
            }
            break;
        case "previous_textual_value":
            fields.Add(update.Name.ToString(), update.Value.ToString());
            break;
    }
}
if (fields.Count > 0)
{
    records.Add(fields);
    fields.Clear();
}
}

I was able to get to the activity logs by the following code:

var response = await client.SendAsync(request);|
string responseText = await response.Content.ReadAsStringAsync();|
var responseData = JObject.Parse(responseText).SelectToken(data).SelectToken(boards).SelectToken(activity_logs);|

Hello @Meneghini !

Being transparent with you, I do not have experience using C#, but maybe this post, or some other posts about it in the community might be of use 🙂

Cheers,
Matias


Figured out the issue. The Activity Logs come back with \\" so the file contains \\\\\\". I did a replace that took out the extra two slashes. I then had to do a replace “{ with {
and a replace }” with }
This let me parse the data as Json and easily loop through the data.

This was a tough thing to figure out.
Hope this helps someone in the future.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.