-1

I receive a JObject object form a thoird party service which contains the below shown JSON:

{
    {
        "topic": "asd",
        "id": "b87a5db6-01b5-4cc0-8fa7-832382ca3431",
        "eventType": "update",
        "subject": "/asd/task",
        "eventTime": "12/18/2018 4:46:20 PM",
        "data": {
            "auditId": "-9223372036854771584",
            "contextData": "{}",
            "eventType": null,
            "auditData": {
                "taskId": "22BC1515-39DF-4124-89EB-AD1468572F20",
                "user": "asd@ul.com",
                **"reminderDate": null,
                "startDate": null,
                "dueDate": null,**
                "taskStatusName": "NotScheduled"
            }
        },
        "dataVersion": "",
        "metadataVersion": "1"
    }
}

This JObject may or may not have the JTokens which are highlighted in bold.

I need to see if such JTokens are present in the incoming JObject or not.

If they are present, then I need to take values from them. For e.g. here reminderDate, startDate and dueDate JTokens are present so I need to fetch their respective values which is null for this example.

If these Jtokens are not present in JObject, then I need to take the values from some other source (like DB).

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
  • Welcome to StackOverflow. Thank you for your well-defined example of input and desired output. Since this is a Q&A site, and not a code-writing service, it would be beneficial to also include some code you've tried, and a description of how it doesn't work the way you want. If you simply don't know where to begin, I recommend reading [this](https://www.newtonsoft.com/json/help/html/SelectToken.htm). – StriplingWarrior Dec 18 '18 at 17:39
  • this is what I am trying to do: eventGridEvent["data"].SelectToken("auditData")["startDate"] != null Where eventGridEvent is my JObject. Is this the correct way of checking if the startDate JToken is present in this JObject? – sparsh agarwal Dec 18 '18 at 17:44
  • I would use `eventGridEvent.SelectToken("data.auditData.startDate") != null`, since that will handle things more gracefully if there's no `data` or `auditData` property. – StriplingWarrior Dec 18 '18 at 17:50
  • 1
    Seems like a duplicate of [Json.NET get nested jToken value](https://stackoverflow.com/q/42290485/3744182), which suggests to use `SelectToken()` or, in c# 6.0 or later, the null conditional operator. Agree? – dbc Dec 18 '18 at 18:55
  • Thanks @StriplingWarrior – sparsh agarwal Dec 26 '18 at 19:43
  • Thanks @dbc for looking into this – sparsh agarwal Dec 26 '18 at 19:44

1 Answers1

0

To get Property like startDate it will return its value as string or null if not found

check this

    string Text =
    @"{
    ""topic"": ""asd"",
    ""id"": ""b87a5db6-01b5-4cc0-8fa7-832382ca3431"",
    ""eventType"": ""update"",
    ""subject"": ""/asd/task"",
    ""eventTime"": ""12/18/2018 4:46:20 PM"",
    ""data"": {
    ""auditId"": ""-9223372036854771584"",
    ""contextData"": ""{}"",
    ""eventType"": null,
    ""auditData"": {
        ""taskId"": ""22BC1515-39DF-4124-89EB-AD1468572F20"",
        ""user"": ""asd@ul.com"",
        ""reminderDate"": null,
        ""startDate"": null,
        ""dueDate"": null,
        ""taskStatusName"": ""NotScheduled""
    }
    },
    ""dataVersion"": """",
    ""metadataVersion"": ""1""
    }"; 

    JObject response = JObject.Parse(Text);
    string startDate = (string) response ["data"]["auditData"]["startDate"];
Ahmed Ghoniem
  • 661
  • 1
  • 8
  • 15