#Embed and JSON objects array

1 messages · Page 1 of 1 (latest)

fossil hornet
#

Hi,
This question is both DNET Non-DNET related, so not sure whether I should be asking about it here, apologies if not!
I'm trying to generate multiple embeds, and to populate them with data extracted from a JSON file. The JSON file is basically a single array of objects (with the same fields for each object).
The first value of each object is a unique ID.

How could I read, select and assign all values contained in an object, depending on its ID value?
I've tried first to read the file, then to assign the whole array to a variable (I'm using the Newtonsoft.Json NuGet package):

        string text = File.ReadAllText(@"./objects.json");
        //var objectsjson = JsonConvert.DeserializeObject(text);
        Console.WriteLine(text);
        //Console.WriteLine(objectsjson);
```But then I'm stuck, I can't seem to select a single value, for example by writing `text[0]` only the bracket would show up, instead of the first value which should be an ID.

So I think first I need to get the syntax right, and then figure out how to select an object based on its ID and finally store its values in a variable so as to then add them to the embed
obtuse abyss
fossil hornet
#

I've removed the Newtonsoft.Json NuGet package and used the default system JSON serializer as suggested
Had already generated the class from the JSON file via pasting and choosing the "Paste Special" feature
Then went for the same steps again, read the JSON file, assigned it to a variable, and tried to deserialize as suggested
Unfortunately I've got an error when trying to do so: Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to testJSON1.CardJson. Path: $ | LineNumber: 0 | BytePositionInLine: 1. (CardJson represents the class for each object)
Not sure why it can't convert the text when the other JSON tool could do it, to be fair

The JSON file looks like this: [{ "_id": { "$oid": "63e3f30da84d60ca1c4469cd" }, "Name": "Name A", "Rarity": 0, "Description": "some description", "Command": "some command", "cardUrl": "a link", "imageURL": "another link" },{ "_id": { "$oid": "63e3f30da84d60ca1c4469ce" }, "Name": "Name B", "Rarity": 1, "Description": "another description", "Command": "another command", "cardUrl": "another link", "imageURL": "another link" },{ . . . }]

#

Tried this way:

        var objectsjson = JsonSerializer.Deserialize<CardJson>(text);
        Console.WriteLine(objectsjson);
fossil hornet
#

After browsing through Stackoverflow I've tried to rather deserialize it as a list:

        var objectsjson = JsonSerializer.Deserialize<List<CardJson>>(text);
        Console.WriteLine(objectsjson);
```No error so far, but also nothing in the output
fossil hornet
#

Could get back to it today, so I've slightly changed the format of the JSON file as well as the fields and class variables, it now works fine (I can access the different values) Console.WriteLine($"Name:{objectsjson[0].Name}");
Now back to the objective: how could I return all values of a single object by filtering the result with the ID value I provide?
Example: the embed will basically return all values for a single object based on the slash command used
Is there a way to filter the results?
I am starting to try a few things with a foreach loop:

        foreach (var o in objectsjson)
        {
            Console.WriteLine(o.Name);
        }
``` this returns all the values
Maybe I can include a "if _ == 63e3f30da84d60ca1c4469cd then get the following values" and then specify them
Not sure what would be a good practice, will continue my research
#

Probably will try to store the objects as an array, or first convert them all as arrays and then filter by ID value

fossil hornet
#

Could solve this by using dictionaries instead:

        var testvalues1 = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(textt);

        for (int i = 0; i < testvalues1.Count; i++)
        {
            //foreach (var stt in testvalues1[i])
            //{
            //    Console.WriteLine(stt);
            //}
            foreach (KeyValuePair<string, object> kvp in testvalues1[i])
            {
                //Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
                Console.WriteLine($"{kvp.Key}" + ": " + $"{kvp.Value}");
            }
        }
```Now I'd just need some help regarding how to implement that to my future slash commands (1 slash command would post 1 embed) so that each one of them only selects one object according to its Id value, and then writes the data the same way but formatted so as to follow the rich embed's design

Also, is it possible to have a slash command attribute with a JSON deserialized value instead of a simple string as the command input? As the value is also a string