C
C#14mo ago
kyeede

❔ Creating a json array and reading a specific property

Hello! So i have a json file as followed
{
"embeds": [
{
"embed1": {
"title": "test number 1",
"description": "test",
"type": "rich",
"color": 79376
},
"embed2": {
"title": "test number 2",
"description": "test",
"type": "rich",
"color": 79376
},
"embed3": {
"title": "test number 3",
"description": "test",
"type": "rich",
"color": 79376
}
}
]
}
{
"embeds": [
{
"embed1": {
"title": "test number 1",
"description": "test",
"type": "rich",
"color": 79376
},
"embed2": {
"title": "test number 2",
"description": "test",
"type": "rich",
"color": 79376
},
"embed3": {
"title": "test number 3",
"description": "test",
"type": "rich",
"color": 79376
}
}
]
}
I want to read let's say everything from embed1 as if it was its own file. The thing to consider is that it has to be a string since the parameter is a string since im reading it like this
var parseObj = (JObject)JsonConvert.DeserializeObject($"App/Data/Configuration/embed.json");

EmbedBuilderUtils.TryParse(parseObj.SelectToken("embeds[0].embed1").ToObject<string>(), out var builder);
var parseObj = (JObject)JsonConvert.DeserializeObject($"App/Data/Configuration/embed.json");

EmbedBuilderUtils.TryParse(parseObj.SelectToken("embeds[0].embed1").ToObject<string>(), out var builder);
I am getting this error and idk what to do... Unexpected character encountered while parsing value: A. Path '', line 0, position 0..
20 Replies
Angius
Angius14mo ago
Just deserialize it all proper-like instead of rawdogging it with JObject?
kyeede
kyeedeOP14mo ago
Did exactly that. My code is now
var parseObj = JObject.Parse(File.ReadAllText($"App/Data/Configuration/embed.json"));

EmbedBuilderUtils.TryParse(parseObj["embeds"][0]["embed2"].ToString(), out var builder);
var parseObj = JObject.Parse(File.ReadAllText($"App/Data/Configuration/embed.json"));

EmbedBuilderUtils.TryParse(parseObj["embeds"][0]["embed2"].ToString(), out var builder);
code seems to work now as intended
Angius
Angius14mo ago
You're still rawdogging it with JObject for some godforsaken reason
kyeede
kyeedeOP14mo ago
thonk wdym
Angius
Angius14mo ago
You should deserialize it to a proper class So you don't need to use magic strings that are prone to typos and what not
kyeede
kyeedeOP14mo ago
so your solution is to simply just create a class holding these values and assign them default values incase they aren't used?
Angius
Angius14mo ago
Are they ever not used?
kyeede
kyeedeOP14mo ago
well the thing is, EmbedBuilderUtils holds a lot of values that I don't use such as Fields[] Thumbnails, Images, Footer
Angius
Angius14mo ago
You can only have the properties you do use And deserialize to that
kyeede
kyeedeOP14mo ago
may i ask, what exactly is considered a bad practice for my code? is there a certain reason this approach is not good? im assuming the way its used could be subject to a lot of issues
MODiX
MODiX14mo ago
Angius
REPL Result: Success
using System.Text.Json;

class Foo
{
public string Name { get; set; }
}

var json = """
{
"Name": "Bob",
"Age": 69
}
""";

var data = JsonSerializer.Deserialize<Foo>(json);
data
using System.Text.Json;

class Foo
{
public string Name { get; set; }
}

var json = """
{
"Name": "Bob",
"Age": 69
}
""";

var data = JsonSerializer.Deserialize<Foo>(json);
data
Result: Foo
{
"name": "Bob"
}
{
"name": "Bob"
}
Compile: 585.296ms | Execution: 57.417ms | React with ❌ to remove this embed.
Angius
Angius14mo ago
Typos would be one thing In my example, you can access data.Name With autocomplete and all that good stuff Not data["Nmae"] which would just be a runtime crash
kyeede
kyeedeOP14mo ago
i do use that approach as part of my project the only reason i didn't use it here is because the embed needs to have certain things in there to be fully parsed i would clutter the class with default values which would then be output to my embed which i have no control over
Angius
Angius14mo ago
Write the class once and forget about it ¯\_(ツ)_/¯ But sure, whatever works
kyeede
kyeedeOP14mo ago
i will see if i can create it and go on from there thank you!!! hug
Angius
Angius14mo ago
$jsongen
MODiX
MODiX14mo ago
Use https://app.quicktype.io or https://json2csharp.com to generate classes from your JSON
Instantly parse JSON in any language | quicktype
Whether you're using C#, Swift, TypeScript, Go, C++ or other languages, quicktype generates models and helper code for quickly and safely reading JSON in your apps. Customize online with advanced options, or download a command-line tool.
Convert JSON to C# Classes Online - Json2CSharp Toolkit
Convert any JSON object to C# classes online. Json2CSharp is a free toolkit that will help you generate C# classes on the fly.
Angius
Angius14mo ago
You can just generate it think
kyeede
kyeedeOP14mo ago
bookmarked
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server