C#
C#

help

Root Question Message

Doktor9
Doktor912/12/2022
❔ How can I query a JSON doc for only objects with a specific property value?

I have a JSON object that looks like this:
{
    "pagedata": [
        {
            "id": "settings_tab_1",
            "data": {
                "oneSetting": {
                    "id": "InitialExperience",
                    "value": "1505209",
                    "options": [
                        {
                            "value": "option1"
                        }
                    ]
                },
                ...
        },
        {
            "id": "settings_tab_5",
            "data": {
                "someOtherSetting": {
                    "id": "ResultSortOrder",
                    "value": "1064235",
                    "options" [
                        {
                            "value": "option1"
                        }
                    ]                
                },
                ...
            }
        }
    ],
    "selectedTabId": "settings_tab_5",
    "statuscode": "OK",
    "props": {
        "type": "settings",
        "baseurl": "/settings"
    }
}

The pagedata array here has two items, with id values of settings_tab_1 and settings_tab_5. How could I extract a version of this JSON with only one pagedata item, the one with an id value of settings_tab_5?
Angius
Angius12/12/2022
Deserialize and LINQ
Doktor9
Doktor912/12/2022
My first line of thought is to use LINQ before the data gets serialized, but I'm not certain to what extent I will be able to change the fetch & serialize code. I would prefer to avoid serializing, deserializing just to query, and then serializing again to return to the client. I guess it comes down to whether that brings a higher perf cost than filtering the JSON in place.
Angius
Angius12/12/2022
JSON will have to be converted into something understandable by C# one way or another
Sunder
Sunder12/12/2022
deserializing the whole thing would probably the simplest and probably even the fastest
Sunder
Sunder12/12/2022
if you want to filter it before deserializing you could do something like this
Sunder
Sunder12/12/2022
var items = JsonSerializer
    .Deserialize<JsonElement>("json goes here")
    .GetProperty("pagedata")
    .EnumerateArray()
    .Where(element => element.GetProperty("id").GetString() == "settings_tab_5")
    .Select(element => element.Deserialize<Model>());
Sunder
Sunder12/12/2022
which is most likely slower then just deserializing
Doktor9
Doktor912/12/2022
No, it comes from C# before I want to filter it, but then it's destined for an Angular client which has one deserialize method per pagedata item as they all have different shapes
Doktor9
Doktor912/12/2022
Thank you, it seems JsonElement is key to my last resort tactic, but in your example, in Deserialize<Model>, what is Model?
Doktor9
Doktor912/12/2022
That does look like some awfully slow LINQ though (no offence, it's not your fault, it's LINQ's fault), so I am probably better off with first just deserializing the whole object.
Sunder
Sunder12/12/2022
Model is a class with whatever properties you want
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy