I have not typical json that I get when request for profile with userName (in example I show what will be if I query with username1
. If I query with username2
then property name is "field_set_key=\"profile\",username=\"username2\""
"UserProfileResource": {
"field_set_key=\"profile\",username=\"username1\"": {
"data": {
"profile": {
...
}
}
}
}
I cannot simply set JsonProperty
with some name as it is dynamic. So I need to parse it manually somehow.
Yes, it looks simple if I know what profile is requested (what username passed).
Just parsed json string into some JObject
, build that dynamic property name and get it's value using LINQ to JSON.
But what can be done in case I don't know username that are requested? Can I get property value, which name contains some string (like field_set_key=\"profile\"
) using mentioned above LINQ to JSON as example?
As @ZoharPeled said in comment, I can use use JsonPath to query json using SelectToken As shown in Querying JSON with SelectToken
var jObject = JObject.Parse(json);
var userProfile = jObject.SelectToken("UserProfileResource.*.data.profile").ToObject<UserProfile>();
In example I parsed my json to JObject
and from it select profile data using SelectToken
. As you can see, I also used JSONPath expressions there.
*
means
wildcard. All objects/elements regardless their names.