Randeep Singh
Randeep Singh

Reputation: 1018

JToken get a specific value

I have following JToken output. How can I retrieve the 'value' here from TenantID which should be 1 in this case?

{[
 {
  "value": 1,
  "metadata": {
   "userType": 0,
   "flags": 8,
   "type": {
    "type": "INT4",
    "name": "Int",
    "id": 56
   },
   "colName": "TenantID"
  }
 }
]}

This is my current code :

        JToken value;
        if (usr.Profile.TryGetValue("tenantid", out value))
        {
            JObject inner = value["value"].Value<JObject>(); //not working with null error
            User.TenantID = (string)value;
        }
        User.obj = usr.Profile;

EDIT - please find below the complete JToken output :

    {[
    {
    "value": 1,
    "metadata": {
      "userType": 0,
      "flags": 8,
      "type": {
        "type": "INT4",
        "name": "Int",
        "id": 56
      },
      "colName": "TenantID"
    }
    }
    ]}
    Count: 1
    First (Newtonsoft.Json.Linq.JContainer): {{
  "value": 1,
  "metadata": {
    "userType": 0,
    "flags": 8,
    "type": {
      "type": "INT4",
      "name": "Int",
      "id": 56
    },
    "colName": "TenantID"
  }
}}
    First (Newtonsoft.Json.Linq.JToken): {{
  "value": 1,
  "metadata": {
    "userType": 0,
    "flags": 8,
    "type": {
      "type": "INT4",
      "name": "Int",
      "id": 56
    },
    "colName": "TenantID"
  }
}}
    HasValues: true
    IsReadOnly: false
    Last (Newtonsoft.Json.Linq.JContainer): {{
`"value": 1,
  "metadata": {
    "userType": 0,
    "flags": 8,
    "type": {
      "type": "INT4",
      "name": "Int",
      "id": 56
    },
    "colName": "TenantID"
  }
}}
    Last (Newtonsoft.Json.Linq.JToken): {{
  "value": 1,
  "metadata": {
    "userType": 0,
    "flags": 8,
    "type": {
      "type": "INT4",
      "name": "Int",
      "id": 56
    },
    "colName": "TenantID"
  }
}}
    Next: (null)
    Parent: {"tenantid": [
  {
    "value": 1,
    "metadata": {
      "userType": 0,
      "flags": 8,
      "type": {
        "type": "INT4",
        "name": "Int",
        "id": 56
      },
      "colName": "TenantID"
    }
  }
]}
    Path: "tenantid"
    Previous: (null)
    Static members: 
    Non-public members: 
    IEnumerator: 
    Root: {{
  "name": "Rx Sidhu",
  "given_name": "Rx",
  "family_name": "Sidhu",
  "locale": "en_US",
  "emails": [
    "[email protected]",
    "[email protected]",
    "[email protected]"
  ],
  "nickname": "[email protected]",
  "email": "[email protected]",
  "picture": "https://apis.live.net/v5.0/f1xxxxxx/picture",
  "roles": [
    "Account Admin",
    "Admin"
  ],
  "tenantid": [
    {
      "value": 1,
      "metadata": {
        "userType": 0,
        "flags": 8,
        "type": {
          "type": "INT4",
          "name": "Int",
          "id": 56
        },
        "colName": "TenantID"
      }
    }
  ],
  "email_verified": true,
  "clientID": "wxxxvC8",
  "updated_at": "2015-07-15T16:26:30.526Z",
  "user_id": "windowslive|f1axxxac",
  "identities": [
    {
      "access_token": "EwBwAq1",
      "provider": "windowslive",
      "user_id": "f1aexxxxxac",
      "connection": "windowslive",
      "isSocial": true
    }
  ],
  "created_at": "2015-07-01T06:08:21.358Z"
}}
    Type: 2

I need to check if the tenantID actually exists and then get value, else return null or 0.

Upvotes: 5

Views: 21489

Answers (3)

Randeep Singh
Randeep Singh

Reputation: 1018

I got what I was looking for :

JToken value;
        if (usr.Profile.TryGetValue("tenantid", out value))
        {
            User.TenantID = (int)value [0] ["value"];
        }

Upvotes: 0

Ghasan غسان
Ghasan غسان

Reputation: 5857

I still cannot get the overall picture of your JSON, though have a look at this. It may help you.

var str = @"{
    ""x"": [{
            ""value"": 1,
            ""metadata"": {
                ""userType"": 0,
                ""flags"": 8,
                ""type"": {
                    ""type"":""INT4"",
                    ""name"":""Int"",
                    ""id"": 56
                },
                ""colName"":""TenantID""
            }
        }
    ]
}";

var parentJObject = JObject.Parse(str);
var xJArray = (JArray)parentJObject["x"];

// first item in JArray which is the object of interest
// look for the appropriate index of the JObject of your data
var firstJTokenInxJArray = (JObject)xJArray[0];

Console.WriteLine(firstJTokenInxJArray["value"].ToString());

Upvotes: 0

Greg
Greg

Reputation: 11480

You should be able to do:

JObject jObject = JObject.Parse(...);
JToken value = jObject.SelectToken("value");

You parse your object, then the inner contents should be exposed in which you can leverage the SelectToken method to find that specific value.

To build it out a bit, you could potentially do:

public static JToken FindToken<T>(string key, T value)
{
     string serialized = NewtonsoftJsonSerializer.Instance.Serialize(value);
     var jObject = JObject.Parse(serialized);
     var jToken = jObject.SelectToken(key);

     if(jToken != null)
          return jToken;

     return null;
}

Upvotes: 6

Related Questions