0

Something is making my head in,

I have an API that returns me JSON, the JSON can have a slight different format depending on the endpoint is sending it. Example.

{
  "PayLoad": {
    "Method1": [
      {
        "TimeStamp": "2020-06-03T13:25:25",
        "Id": 4235411
      }
    ],
    "Timestamp": "2020-06-03T13:26:57.1316371+00:00",
    "Signature": "113af0a218b0497ff6f160fcd1b13a7b",
    "Hookid": "526ed776-2l71-4c2a-b11f-de8cb2057b1c"
  }
}

So what I am looking for is to have returned

Method : Method1
TimeStamp: 2020-06-03T13:25:25
id: 4235411
Signature: 113af0a218b0497ff6f160fcd1b13a7b
HookID: 526ed776-2l71-4c2a-b11f-de8cb2057b1c

Can someone please give me an hint?

I've tried a bunch of code examples I've seen, and can't make it work.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
APearTree
  • 51
  • 1
  • 8
  • 1
    How do you know that "Method1" signifies a "Method"? How do you decide to show the "TimeStamp" from "Method1" vs the "Timestamp" value under "PayLoad"? What do these other formats look like and how do you expect your output to change, if at all? – Heretic Monkey Jun 03 '20 at 15:59
  • 1
    Create classes based on the JSON, deserialize it and create a new object based on the response. – Jawad Jun 03 '20 at 16:05
  • @HereticMonkey I just need the TimeStamp from the Method1(on this case), the difference between them is that one is when the event was generated and the other is when the webhook sent the data to us. – APearTree Jun 03 '20 at 16:15
  • 1
    Does this answer your question? [Deserialize json object into dynamic object using Json.net](https://stackoverflow.com/questions/4535840/deserialize-json-object-into-dynamic-object-using-json-net) – Pavel Anikhouski Jun 03 '20 at 16:32

2 Answers2

0

Without getting more information it is hard to know exactly what you are asking but I would look at the examples in JSON.net's Linq to JSON documentation:

https://www.newtonsoft.com/json/help/html/LINQtoJSON.htm

https://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm

string json = @"
{
    'PayLoad': {
        'Method1':
            [
                {
                'TimeStamp': '2020-06-03T13:25:25',
                'Id': 4235411
                }
            ],
        'Timestamp': '2020-06-03T13:26:57.1316371+00:00',
        'Signature': '113af0a218b0497ff6f160fcd1b13a7b',
        'Hookid': '526ed776-2l71-4c2a-b11f-de8cb2057b1c'
    }
}";

JObject obj = JObject.Parse(json);

string method1_timestamp = (string)obj["PayLoad"]["Method1"][0]["TimeStamp"];
// "2020-06-03T13:25:25"
// Use technique to parse out other values if needed
WillC
  • 1,761
  • 17
  • 37
0

You can try creating a class compatible with the expected JSON format:

public class Payload
{
   public int Id {get; set;}
   public Guid HookID {get; set;}
   public string Signature {get; set;}
   public Method[] Methods {get; set;}
   public DateTime TimeStamp {get; set;}
}

public class Method
{
   public int Id {get; set;}
   public DateTime TimeStamp{get; set;}
}

Than you can parse the JSON data as a Payload object.

Payload payload = JsonConvert.DeserializeObject<Payload>(json);
charbel
  • 471
  • 1
  • 5
  • 18