❔ 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 Kelly2y 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 Kelly2y 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
More Posts
❔ Are TryAsync methods acceptable?I've just been refactoring some implementations of a method I have to go from `T Load()` to `bool Tr❔ Wpf chess board problemHello! Im trying to color a chess board. I have a bunch of border tags for the squares. I think it c❔ Errors when everything looks ok.So, I'm dumb a aspnet api, with controllers, using sqlite. There are no syntax errors in the code, s✅ How do I merge a list of dictionaries based on keys using LINQ?So if I have something like: `[{ number: 1 }, { number: 2 }, { number: 3 }]` I want to get: `{ numbeSelect Hotkey Component WinformsHow can i have a sort of "select hotkey" component that lets the user press a key and captures the K❔ Using compact switch syntaxIs it possible to set `format_code = 1` for case `_` using C#8.0 switch syntax? `// int format_code✅ Using Activator.CreateInstance() when the constructor has arguments - not finding constructorI'm trying to create a class instance dynamically using `Activator.CreateInstance()`, and it's crash❔ Creating a list to loop through school grades & pointsI'm trying to create a list to save my grades & points and loop through them to be able to account t❔ Creating a class that takes a Logger for both the class and its base classI'm trying to figure out the best way to use Loggers in my app. All of the documentation that I've ❔ Basic question on linear search in listHello, I am making a blog/notebook for an assignment and I have a list which contains vectors with 3