0

I have the following JSON that I get back from a service.

See sample of three records:

{"test1@gmail.com":[{"action":"open","timestamp":"2018-09-05 20:46:00","url":null,"ip":"66.102.6.98"}]}

{"test2@gmail.com":[{"action":"open","timestamp":"2018-09-05 18:01:29","url":null,"ip":"66.102.8.129"}]}

{"test3@gmail.com":[{"action":"open","timestamp":"2018-09-05 15:08:26","url":null,"ip":"66.102.6.109"}]}

The first key is always changing, so what is the best way to go convert this into a .NET object?

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
  • 1
    Show us what have you tried? – PNDev Sep 06 '18 at 00:15
  • Do you mean using a dynamic object? [Deserialize json object into dynamic object using Json.net](https://stackoverflow.com/questions/4535840/deserialize-json-object-into-dynamic-object-using-json-net) or [Deserialize JSON into C# dynamic object?](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) – jonatasmello Sep 06 '18 at 03:35

2 Answers2

0

I'm not sure if there is any other way to deserialize the json when its key is different. When key is different it is considered as another type. You can do a work around like stripping the constant part of the Json using regex and deserialize it.

Try if this helps

    public class CommonPart
{

    [JsonProperty("action")]
    public string action { get; set; }

    [JsonProperty("timestamp")]
    public string timestamp { get; set; }

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

    [JsonProperty("ip")]
    public string ip { get; set; }
}

public class EmailPart
{
    public string Email { get; set; }
    public IList<CommonPart> Model { get; set; }
}

Then use a method to get the json like below

public EmailPart GetFromJson(string json)
    {
        var pattern = @"\[(.*?)\]";
        var regex = new Regex(pattern);
        var response = regex.Match(json);
        var test1GmailCom = response.Value;
        var responseModel = JsonConvert.DeserializeObject<List<CommonPart>>(test1GmailCom);

        var ex = new EmailPart();
        ex.Model = responseModel;

        var pattern2 = @"\'(.*?)\'";
        var regex2 = new Regex(pattern2);
        var email = regex2.Match(json).Value.Replace("'", string.Empty);

        ex.Email = email;

        return ex;

    }

I don't think this is the best way. It is best if you can update the Json, May be as below

{ "Data": { "test1@gmail.com": [ { "action": "open", "timestamp": "2018-09-05 20:46:00", "url": null, "ip": "66.102.6.98" } ] } }

Akbar Badhusha
  • 2,415
  • 18
  • 30
0

With Cinchoo ETL - an open source library, you can load your JSON to .NET objects as shown below

.net object definition

public class ActionMessage
{
    public string Action { get; set; }
    public DateTime Timestamp { get; set; }
    public string Url { get; set; }
    public string IP { get; set; }

}

JSON parsing code

string json = @"{
    ""test1@gmail.com"": [
        {
            ""action"": ""open"",
            ""timestamp"": ""2018-09-05 20:46:00"",
            ""url"": ""http://www.google.com"",
            ""ip"": ""66.102.6.98""
        }
    ]
}";
using (var p = ChoJSONReader<ActionMessage>.LoadText(json)
    .WithJSONPath("$.*")
    )
{
    foreach (var rec in p)
    {
        Console.WriteLine("action: " + rec.Action);
        Console.WriteLine("timestamp: " + rec.Timestamp);
        Console.WriteLine("url: " + rec.Url);
        Console.WriteLine("ip: " + rec.IP);
    }
}

Output:

action: open
timestamp: 9/5/2018 8:46:00 PM
url: http://www.google.com
ip: 66.102.6.98
Cinchoo
  • 6,088
  • 2
  • 19
  • 34