❔ How can I query a JSON doc for only objects with a specific property value?
I have a JSON object that looks like this:
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
Deserialize and LINQ
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.
JSON will have to be converted into something understandable by C# one way or another
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
which is most likely slower then just deserializing
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.Model is a class with whatever properties you want
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.