C
C#14mo ago
mrdudebro1

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.
{
"LicenseUsageList": [
{
"UserName": "doman\\username",
"LicenseType": "license type",
"AcquisitionTime": "2023-10-12T08:04:52.523",
"SessionType": "UISERVER-FULL",
"SessionId": "session id",
"LicenseProperties": [
{
"Key": "DbServerPid",
"Value": "64"
},
{
"Key": "ProgramName",
"Value": "program name"
},
{
"Key": "DbExecutionContextId",
"Value": "0"
},
{
"Key": "Name",
"Value": "app name"
},
{
"Key": "LoginName",
"Value": "user name"
},
{
"Key": "LoginTime",
"Value": "10/12/2023 08:04:52"
},
{
"Key": "HostName",
"Value": "server name"
},
{
"Key": "HostPid",
"Value": "hostPid"
}
]
},
]
}
{
"LicenseUsageList": [
{
"UserName": "doman\\username",
"LicenseType": "license type",
"AcquisitionTime": "2023-10-12T08:04:52.523",
"SessionType": "UISERVER-FULL",
"SessionId": "session id",
"LicenseProperties": [
{
"Key": "DbServerPid",
"Value": "64"
},
{
"Key": "ProgramName",
"Value": "program name"
},
{
"Key": "DbExecutionContextId",
"Value": "0"
},
{
"Key": "Name",
"Value": "app name"
},
{
"Key": "LoginName",
"Value": "user name"
},
{
"Key": "LoginTime",
"Value": "10/12/2023 08:04:52"
},
{
"Key": "HostName",
"Value": "server name"
},
{
"Key": "HostPid",
"Value": "hostPid"
}
]
},
]
}
I'd like to access each node via the "UserName" property and find duplicate values
18 Replies
Azrael
Azrael14mo ago
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":…
reflectronic
reflectronic14mo ago
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 want
mrdudebro1
mrdudebro1OP14mo ago
ok. let me serialize my json string to json object basically i want to get the username to check for duplicate logins
reflectronic
reflectronic14mo ago
duplicate UserNames inLicenseUsageList?
mrdudebro1
mrdudebro1OP14mo ago
yes
reflectronic
reflectronic14mo ago
then yeah you can just do something like
var groups = obj["LicenseUsageList"].AsArray().ToLookup(e => e["UserName"].GetValue<string>());
if (groups.Any(a => a.Count() != 1)) { ... }
var groups = obj["LicenseUsageList"].AsArray().ToLookup(e => e["UserName"].GetValue<string>());
if (groups.Any(a => a.Count() != 1)) { ... }
or whatever maybe something more elegant than that, but, it should demonstrate what you nee to do
mrdudebro1
mrdudebro1OP14mo ago
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?
reflectronic
reflectronic14mo ago
it returns another JsonNode the one representing the value. so, in this case, it should actually be a JsonArray
mrdudebro1
mrdudebro1OP14mo ago
ohhhh okay can it be a jsonnode object? i have it converted from a plain string to a JsonNode by using JsonObject.Parse(jsonString)
reflectronic
reflectronic14mo ago
can what be a jsonnode
mrdudebro1
mrdudebro1OP14mo ago
copy your code directly var groups = jsonNode["LicenseUsageList"].AsArray().ToLookup(e => e["UserName"].GetValue<string>());
reflectronic
reflectronic14mo ago
obj is a JsonNode, yeah
mrdudebro1
mrdudebro1OP14mo ago
whereis that ToLookup() function from? a parent class?
reflectronic
reflectronic14mo ago
it's from LINQ using System.Linq;
mrdudebro1
mrdudebro1OP14mo ago
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 linq
reflectronic
reflectronic14mo ago
yes
mrdudebro1
mrdudebro1OP14mo ago
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"
Want results from more Discord servers?
Add your server