C
C#15mo 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
Alisa15mo 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
Pobiega15mo ago
so umh you just want to get the keys from the root object? and ignore the values?
Alisa
Alisa15mo 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
Pobiega15mo 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
Alisa15mo 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
Pobiega15mo 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
Alisa15mo ago
it makes more sense for the app anyway oh you already did it sorry hon I wasted your breath 😭
Pobiega
Pobiega15mo ago
¯\_(ツ)_/¯ regardless, you should be doing this with serialization instead of JsonObject
Alisa
Alisa15mo ago
hmmm
Pobiega
Pobiega15mo ago
var data = JsonSerializer.Deserialize<Dictionary<string, string[]>>(json);
Alisa
Alisa15mo ago
that makes me have to create types to serialize into
Pobiega
Pobiega15mo ago
thats it. nope.
Alisa
Alisa15mo ago
oh.
Pobiega
Pobiega15mo ago
the json above is a dictionary with string keys and string array/lists
Alisa
Alisa15mo 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
Pobiega15mo ago
its a general purpose language. it has to support everything there will always be more than one way of doing something
Alisa
Alisa15mo ago
that's why ahk is my favorite language!
ahk! support everything but actually anything useful!
Pobiega
Pobiega15mo ago
System.Text.Json is just a library, you could write your own json library if you wanted.
Alisa
Alisa15mo ago
I shit you not it has a way to sort strings but not arrays 💀
Pobiega
Pobiega15mo ago
¯\_(ツ)_/¯
Alisa
Alisa15mo ago
it's very in character of me ahahahahahah I wrote a window manager recently
Pobiega
Pobiega15mo 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
Alisa15mo ago
and linq which is exactly what I was missing
Pobiega
Pobiega15mo ago
well, the old one used linq too :p
Alisa
Alisa15mo ago
because I knew about LastOrDefault, and it was exactly what I was looking for
Pobiega
Pobiega15mo ago
LastOrDefault()?.ToString() ?? None
Alisa
Alisa15mo ago
oh nonono I mean in my solution
Pobiega
Pobiega15mo ago
ah
Alisa
Alisa15mo ago
yours is wonderful sweetheart thank you for helping me!!
Pobiega
Pobiega15mo ago
welcome is you.
Alisa
Alisa15mo ago
I feel like we beginners like to overcomplicate things sometimes because we think it's the actual way to do it
Pobiega
Pobiega15mo ago
yes 🙂
Alisa
Alisa15mo ago
I see this from the perspective you're currently in all the time
Pobiega
Pobiega15mo ago
also.. $close
MODiX
MODiX15mo ago
Use the /close command to mark a forum thread as answered
Alisa
Alisa15mo ago
oh cool you can call your bot inside of a message 🤯
Pobiega
Pobiega15mo 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
Alisa15mo 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
Pobiega15mo ago
tags are not commands either, we can add new ones without restarting the bot. and they have ownerships etc. its good.
Alisa
Alisa15mo ago
yeah same .ask is a special command
Pobiega
Pobiega15mo ago
modix source is opensource too, if you want to check how it was done
Alisa
Alisa15mo ago
but we have .tag tagName for the usual stuff
Want results from more Discord servers?
Add your server
More Posts