C
C#2y ago
Alisa

✅ getting all the keys of a json object

The json input I have is:
{
"vacuum": [],
"dust": []
}
{
"vacuum": [],
"dust": []
}
What I want as output is:
vacuum
dust
vacuum
dust
The json is contained in a JsonObject, and I'm trying to make a method to do what I'm talking about:
public void List()
{
foreach (var key in jsonObj)
{
Console.WriteLine(key);
}
}
public void List()
{
foreach (var key in jsonObj)
{
Console.WriteLine(key);
}
}
But the output I'm getting is:
[vacuum, []]
[dust, []]
[vacuum, []]
[dust, []]
42 Replies
Alisa
AlisaOP2y ago
got it
public void List()
{
foreach (KeyValuePair<string, JsonNode?> subObj in jsonObj)
{
Console.WriteLine(subObj.Key);
}
}
public void List()
{
foreach (KeyValuePair<string, JsonNode?> subObj in jsonObj)
{
Console.WriteLine(subObj.Key);
}
}
another question
private List<string> ListChores()
{
uint i = 0;
List<string> chores = new();
foreach (KeyValuePair<string, JsonNode?> subObj in jsonObj)
{
i++;
var lastDate = subObj.Value ?? "None";
chores.Add(i + ") " + subObj.Key + " " + lastDate);
}
return chores;
}
private List<string> ListChores()
{
uint i = 0;
List<string> chores = new();
foreach (KeyValuePair<string, JsonNode?> subObj in jsonObj)
{
i++;
var lastDate = subObj.Value ?? "None";
chores.Add(i + ") " + subObj.Key + " " + lastDate);
}
return chores;
}
each key has a value of an array of strings I want to either get the last string of that array of strings, or if the array is empty, get "None" https://github.com/Axlefublr/ChoreTracker/blob/main/ChoreTracker/ChoreManager.cs
Pobiega
Pobiega2y ago
so umh you just want to get the keys from the root object? and ignore the values?
Alisa
AlisaOP2y ago
done that already ☑️ now I wanna do the exact opposite of this I want to get the last value in the array (which is the value)
Pobiega
Pobiega2y ago
last value in the array for each key? Currently, your arrays are all empty or do you just want to fetch the "value", aka the empty arrays?
Alisa
AlisaOP2y ago
yes! they are currently empty, but aren't going to be eventually if they're empty, I want to get the string "None", if they have at least one value, then the last one I think I know how I wanna approach this instead of storing an array of values, I should just store a single value
Pobiega
Pobiega2y ago
var json =
"""
{
"vacuum": [],
"eat": ["food"],
"dust": ["hello", "goodbye"]
}
""";

var obj = JsonObject.Parse(json).AsObject();

foreach (var keyValuePair in obj)
{
var val = keyValuePair.Value.AsArray();
var outputForValue = val.LastOrDefault()?.ToString() ?? "None";
Console.WriteLine($"{keyValuePair.Key}: {outputForValue}");
}
var json =
"""
{
"vacuum": [],
"eat": ["food"],
"dust": ["hello", "goodbye"]
}
""";

var obj = JsonObject.Parse(json).AsObject();

foreach (var keyValuePair in obj)
{
var val = keyValuePair.Value.AsArray();
var outputForValue = val.LastOrDefault()?.ToString() ?? "None";
Console.WriteLine($"{keyValuePair.Key}: {outputForValue}");
}
Alisa
AlisaOP2y ago
it makes more sense for the app anyway oh you already did it sorry hon I wasted your breath 😭
Pobiega
Pobiega2y ago
¯\_(ツ)_/¯ regardless, you should be doing this with serialization instead of JsonObject
Alisa
AlisaOP2y ago
hmmm
Pobiega
Pobiega2y ago
var data = JsonSerializer.Deserialize<Dictionary<string, string[]>>(json);
Alisa
AlisaOP2y ago
that makes me have to create types to serialize into
Pobiega
Pobiega2y ago
thats it. nope.
Alisa
AlisaOP2y ago
oh.
Pobiega
Pobiega2y ago
the json above is a dictionary with string keys and string array/lists
Alisa
AlisaOP2y ago
I have a feeling this is going to be a recurring theme: why does C# provide a more annoying way to do the same thing then? ahahahhahaahaa
Pobiega
Pobiega2y ago
its a general purpose language. it has to support everything there will always be more than one way of doing something
Alisa
AlisaOP2y ago
that's why ahk is my favorite language!
ahk! support everything but actually anything useful!
Pobiega
Pobiega2y ago
System.Text.Json is just a library, you could write your own json library if you wanted.
Alisa
AlisaOP2y ago
I shit you not it has a way to sort strings but not arrays 💀
Pobiega
Pobiega2y ago
¯\_(ツ)_/¯
Alisa
AlisaOP2y ago
it's very in character of me ahahahahahah I wrote a window manager recently
Pobiega
Pobiega2y ago
var json =
"""
{
"vacuum": [],
"eat": ["food"],
"dust": ["hello", "goodbye"]
}
""";

var data = JsonSerializer.Deserialize<Dictionary<string, string[]>>(json);

foreach (var kvp in data)
{
var outputForValue = kvp.Value.LastOrDefault() ?? "None";
Console.WriteLine($"{kvp.Key}: {outputForValue}");
}
var json =
"""
{
"vacuum": [],
"eat": ["food"],
"dust": ["hello", "goodbye"]
}
""";

var data = JsonSerializer.Deserialize<Dictionary<string, string[]>>(json);

foreach (var kvp in data)
{
var outputForValue = kvp.Value.LastOrDefault() ?? "None";
Console.WriteLine($"{kvp.Key}: {outputForValue}");
}
same code but with a dictionary much better type safety etc
Alisa
AlisaOP2y ago
and linq which is exactly what I was missing
Pobiega
Pobiega2y ago
well, the old one used linq too :p
Alisa
AlisaOP2y ago
because I knew about LastOrDefault, and it was exactly what I was looking for
Pobiega
Pobiega2y ago
LastOrDefault()?.ToString() ?? None
Alisa
AlisaOP2y ago
oh nonono I mean in my solution
Pobiega
Pobiega2y ago
ah
Alisa
AlisaOP2y ago
yours is wonderful sweetheart thank you for helping me!!
Pobiega
Pobiega2y ago
welcome is you.
Alisa
AlisaOP2y ago
I feel like we beginners like to overcomplicate things sometimes because we think it's the actual way to do it
Pobiega
Pobiega2y ago
yes 🙂
Alisa
AlisaOP2y ago
I see this from the perspective you're currently in all the time
Pobiega
Pobiega2y ago
also.. $close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
Alisa
AlisaOP2y ago
oh cool you can call your bot inside of a message 🤯
Pobiega
Pobiega2y ago
yeah the dollarsign stuff is "tags", its a short way to invoke a pre-written message from the bot we have a whole bunch of them for common replies, links, resources etc and memes, ofc
Alisa
AlisaOP2y ago
same same we do on our server as well but there you have to start a message with a dot command like you say .ask and it displays the message but if you say hey you should .ask here instead! it won't work
Pobiega
Pobiega2y ago
tags are not commands either, we can add new ones without restarting the bot. and they have ownerships etc. its good.
Alisa
AlisaOP2y ago
yeah same .ask is a special command
Pobiega
Pobiega2y ago
modix source is opensource too, if you want to check how it was done
Alisa
AlisaOP2y ago
but we have .tag tagName for the usual stuff
Want results from more Discord servers?
Add your server