Json.Net Select Object based on a value

15,308

Since it seems from the comments that you're already semi-comfortable with the idea of using JObject and Linq, here is an example program demonstrating how to get a specific match from your JSON by ID using that approach:

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {
            wvw_matches: [
                {
                    wvw_match_id: ""1-4"",
                    red_world_id: 1011,
                    blue_world_id: 1003,
                    green_world_id: 1002,
                    start_time: ""2013-09-14T01:00:00Z"",
                    end_time: ""2013-09-21T01:00:00Z""
                },
                {
                    wvw_match_id: ""1-2"",
                    red_world_id: 1017,
                    blue_world_id: 1021,
                    green_world_id: 1009,
                    start_time: ""2013-09-14T01:00:00Z"",
                    end_time: ""2013-09-21T01:00:00Z""
                }
            ]
        }";

        string matchIdToFind = "1-2";
        JObject jo = JObject.Parse(json);

        JObject match = jo["wvw_matches"].Values<JObject>()
            .Where(m => m["wvw_match_id"].Value<string>() == matchIdToFind)
            .FirstOrDefault();

        if (match != null)
        {
            foreach (JProperty prop in match.Properties())
            {
                Console.WriteLine(prop.Name + ": " + prop.Value);
            }
        }
        else
        {
            Console.WriteLine("match not found");
        }

    }
}

Output:

wvw_match_id: 1-2
red_world_id: 1017
blue_world_id: 1021
green_world_id: 1009
start_time: 9/14/2013 1:00:00 AM
end_time: 9/21/2013 1:00:00 AM
Share:
15,308
Jazerix
Author by

Jazerix

I'm a happy guy, studying computer science at the University of Southern Denmark. I feel most at home working with C# or PHP -&gt; Laravel. Lately I've also taken an interest in Vue.js.

Updated on June 13, 2022

Comments

  • Jazerix
    Jazerix almost 2 years

    I have a Json object that looks like this:

    {
         wvw_matches: [
              {
                   wvw_match_id: "1-4",
                   red_world_id: 1011,
                   blue_world_id: 1003,
                   green_world_id: 1002,
                   start_time: "2013-09-14T01:00:00Z",
                   end_time: "2013-09-21T01:00:00Z"
              },
              {
                   wvw_match_id: "1-2",
                   red_world_id: 1017,
                   blue_world_id: 1021,
                   green_world_id: 1009,
                   start_time: "2013-09-14T01:00:00Z",
                   end_time: "2013-09-21T01:00:00Z"
              }
         ]
    }
    

    It contains a lot more objects in the array than the example above shows. Anyway, I need to select the Json object based on the wvw_match_id.

    How would I achieve this? :)

  • Jazerix
    Jazerix over 10 years
    isn't it possible without serializing it?
  • evanmcdonnal
    evanmcdonnal over 10 years
    @Jazerix I was just writing an edit about that. Not that I know of, and even if it were I think it's a worse approach.
  • Jazerix
    Jazerix over 10 years
    Well, I would be ever so happy if I could avoid serializing it :3 but if there are no way around it, I guess I'm gonna have to do it :(
  • evanmcdonnal
    evanmcdonnal over 10 years
    @Jazerix I would be shocked if json.NET offered you anyway to do it without serialzing the data. That's not what the library is made for and it would contradict the separation of concerns design principal.
  • Jazerix
    Jazerix over 10 years
    I've been having no problem doing it without so far. Json.net offers its own Linq library. I'm no expert on this area, and I don't mean to offend you in any way, however it makes no sense to me, you can do a JObject.Select, JObject.Where ect... with it, if you have to serialize it first :)
  • evanmcdonnal
    evanmcdonnal over 10 years
    @Jazerix If you're working with a JObject you've already deserialized it. JObject is a native C# object that represents the string literal json. There is no advantage to working with JObject's instead of user defined classes, I happen to prefer using my own classes.
  • Jazerix
    Jazerix over 10 years
  • German
    German almost 5 years
    Just in case not every array item is guaranteed to have a wvw_match_id, I'd slightly modify the where condition to .Where(m => m["wvw_match_id"]?.Value<string>() == matchIdToFind) to avoid a runtime exception.