Access JSON item with variable key name
Asked Answered
B

2

5

The following is my JSON data which happens to be the format supplied by Wikidata, although greatly trimmed down for clarity.

{
"entities": {
    "Q200405": {
        "id": "Q200405",
        "type": "item",
        "claims": "Cheese"
        }
    }
}

I am attempting to access this data within c#. My problem is that the Q200405 is dynamic - it is basically the record number that I am retrieving. For example, another page might give me

{
"entities": {
    "Q123456": {
        "id": "Q123456",
        "type": "item",
        "claims": "Lemon"
        }
    }
}

My best attempt so far was using Json.NET (Newtonsoft) ;

json = "{\"entities\":{\"Q200405\" {\"id\":\"Q200405\",\"type\":\"item\",\"claims\":\"Cheese\"}}}";

var Query = JsonConvert.DeserializeObject<dynamic>(json);
string entities = Query.entities.ToString();

Query = JsonConvert.DeserializeObject<dynamic>(entities);
string entity = Query.Q200405.ToString();

Query = JsonConvert.DeserializeObject<dynamic>(entity);
string id = Query.id.ToString();
string claims = Query.claims.ToString();

This works, but obviously hardcoding Query.Q200405.ToString() is not the ideal solution! I probably should not be doing multiple Deserialize statements to drill down into the data either?

My question is what is the best way to read the above JSON format into a c# program?

Baer answered 18/12, 2014 at 23:10 Comment(3)
Seems like you might want some sort of X-path query for json. Is it fair to assume you don't have the id value when you need it? Or is it a parameter in this case?Severson
Once you get the json into .NET I highly recommend you take a look at restsharp: restsharp.org as far as best practices go.Mckeown
I do not know the ID. The original sample was a lookup of John Hurt on wikidata.org The ID just happens to be their record number.Baer
K
7

Well if you want to put the names into string variables, just use JObject:

string id = "Q200405"; // Or wherever you get that from
string text = "{\"entities...";
var json = JObject.Parse(text);

var claim = json["entities"][id];

string claims = (string) claim["claims"];
string type = (string) claim["type"];
// etc

Basically LINQ to JSON is really what you want here.

EDIT: If you don't know the ID beforehand, you can use:

var json = JObject.Parse(text);
var entities = (JObject) json["entities"];
var entity = entities.Properties().First();
var id = entity.Name;
var claim = (JObject) entity.Value;
string claims = (string) claim["claims"];
string type = (string) claim["type"];
Console.WriteLine(new { claims, type, id });
Kristinakristine answered 18/12, 2014 at 23:14 Comment(7)
That method works but I do not know the id and it varies between records. However, in my case I could simply use string id = text.Substring(14,7); although that does seem a poor way to do things!Baer
@MortimerCat: No, if you don't know the ID but it's the only ID, you can use json["entities"].First - that will get the first child token.Kristinakristine
Yep, thats great. I had missed those First and FirstOrDefault methods. Thanks.Baer
Now that I actually try your latest version I get the error message Cannot access child value on Newtonsoft.Json.Linq.JProperty on the string claims = (string) claim["claims"]; lineBaer
@MortimerCat: Okay, will have a look now.Kristinakristine
@MortimerCat: Fixed now - give that a try. (It works for me with your sample JSON)Kristinakristine
Thanks for taking the time to do that. I have my program working now :)Baer
M
2

This is how you can parse it

class Data
{
    public string id { get; set; }
    public string type { get; set; }
    public string claims { get; set; }
}

var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string,Data>>>(json);
Data data = obj["entities"].FirstOrDefault().Value;
Metacarpus answered 18/12, 2014 at 23:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.