❔ 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"
}
}
{
"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?
7 Replies
Angius
Angius2y ago
Deserialize and LINQ
Brady Kelly
Brady KellyOP2y ago
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
Angius2y ago
JSON will have to be converted into something understandable by C# one way or another
sunder.cc
sunder.cc2y ago
deserializing the whole thing would probably the simplest and probably even the fastest if you want to filter it before deserializing you could do something like this
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>());
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>());
which is most likely slower then just deserializing
Brady Kelly
Brady KellyOP2y ago
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 Thank you, it seems JsonElement is key to my last resort tactic, but in your example, in Deserialize<Model>, what is Model? 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.cc
sunder.cc2y ago
Model is a class with whatever properties you want
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server