Working with jsonnode class
Working with JsonNode class. Is there any function or property to get the various key value pairs? Would jsonDocument be better? I have a string I got from an http request and I need to manipulate the data. One example of the json is below. all other entries are similar. Certain values omitted for security.
I'd like to access each node via the "UserName" property and find duplicate values
18 Replies
C# JSON System.Text.Json - How to extract an object from a dictiona...
Hello,
I am trying to populate embeds with data taken from a JSON file, however I am not familiar with that format.
The JSON file follows this format (it's a single array of objects which all contain the same fields):
[{
"Id":…
foreach
i am not really sure what you mean by "access each node via the "UserName" property," but
JsonArray
and JsonObject
are both ICollections, and you can iterate/use LINQ/etc. on either of them to do whatever queries you wantok. let me serialize my json string to json object
basically i want to get the username to check for duplicate logins
duplicate
UserName
s inLicenseUsageList
?yes
then yeah you can just do something like or whatever
maybe something more elegant than that, but, it should demonstrate what you nee to do
cool thanks. let me dive in
hey sorry man, this a niche little thing that i'm having a hard time googling, what is meant by the part that says
obj["LicenseUsageList"]
what kind of object is being returned by that line?JsonNode.Item[] Property (System.Text.Json.Nodes)
Gets or sets the element at the specified index.
it returns another JsonNode
the one representing the value. so, in this case, it should actually be a JsonArray
ohhhh okay
can it be a jsonnode object? i have it converted from a plain string to a JsonNode by using
JsonObject.Parse(jsonString)
can what be a jsonnode
copy your code directly
var groups = jsonNode["LicenseUsageList"].AsArray().ToLookup(e => e["UserName"].GetValue<string>());
obj
is a JsonNode
, yeahwhereis that
ToLookup()
function from? a parent class?it's from LINQ
using System.Linq;
oh theyre just like general functions that datastructures can use
like arrays etc.
i'll have to look into that more
seems helpful for data manipulation
i'm assuming
groups.any()
is also from linqyes
for some reason the
e
parameter in that statement kept coming back null. idk if i did something wrong, but i have figured a way to push all the usernames into a list
but it'd be nice to skip the moization step of making a list since i already have it in a jsonarray
i just know how to use lists better than linq objects
i'm gonna sort the list alphabetically to check for duplicates
this feels crazy but it'll work lol
can you conver a plain string into a JsonNode object? i'm getting issues after i converted from List<JsonNode>
to List<String>
. I can't convert back
because it only stored the values for "UserName"