C
C#

help

❔ How can I query a JSON doc for only objects with a specific property value?

DDurabilis12/12/2022
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?
AAngius12/12/2022
Deserialize and LINQ
DDurabilis12/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.
AAngius12/12/2022
JSON will have to be converted into something understandable by C# one way or another
Ssunder.cc12/12/2022
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
DDurabilis12/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 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.
Ssunder.cc12/12/2022
Model is a class with whatever properties you want
AAccord12/13/2022
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.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended 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 3Generic api base clientI'm trying to implement a generic api base client using `Polly` and `System.Net.Http.Formatting`. I ❔ Finding which port a WebApplication has bound toI have a .NET web application, and I'm telling it that it can pick any port that it likes (by giving❔ Method WriteToTable(); isn't doing anything to the database fileMy code: ```private void button1_Click(object sender, EventArgs e) { WriteToTable(); ✅ Iteration through a list in csprojI have a csproj file with a next section ```xml <Target Name="PublishRunWebpack" AfterTargets="CoAzure AD; The app is trying to access a service that your organization lacks a service principal forI am running into this error after trying to set up authentication in my blazorwasm app: "The app is❔ async code in cshtml file of asp net framework that target IE browserhi all, i understand that Internet Explorer arent' compatible with async/await, but is there an alte❔ AmazonS3 set up for svgDoes anyone know how to sets the output format to svg with MagickImage? Amazons3✅ Explicit call ToString() when interpolating on primitive (value) typesIs it necessary to call ToString() when interpolating string using primitive (int, float, etc.) or i✅ How do I change background color of a label from a different namespace part of my app?In my main program class I run `Application.Run(new Form1());` how do I access this instance of my w❔ Chat app with cHow do i make a working chat kinda like messenger