C
C#2y ago
Daryl

✅ Managing JSON which varies in size and/or structure?

I'm working on two hobby projects at the moment, both are meant to accept json input but I'm struggling because C# seems quite strict on datatypes and I'm a noob. In one project the JSON I return is massive and seems like it could be broken into multiple classes. In the second project, the JSON structure seems to vary depending on the data being sent. What's the best way to tackle these sorts of things? I appreciate there is a way to copy JSON into Visual Studio it can generate the class for you but I don't want to be tied to Visual Studio and it's still a lot of admin work. For the varying JSON I captured about 100 entries and put them in a text file but I'm not even sure now how to compare them against each other to see if they're the same structure? Also apparently System.Text doesn't support the "dynamic" data type but Newtonsoft.Json is a thing of the past now? Any advice or tips would be great, thank you!
42 Replies
JakenVeina
JakenVeina2y ago
there isn't a way to copy code into Visual Studio and have it generate C# class models for you that's entirely separate tooling also, not wanting to tie yourself to Visual Studio is a rather silly idea. Visual Studio is what you want for .NET development, unless using Visual Studio is impossible for you if you're referring to dynamic the keyword as something System.Text does not support, but Newtonsoft does, that's nonsense. The Dynamic keyword is a feature of the C# language, and has nothing to do with either of those two libraries it's also 100% not relevant for anything you would want to be doing with JSON if you mean "Dynamic" in a colloquial sense, that's also wrong, both System.Text.Json and Newtonsoft.Json support "dynamic" JSON modeling that is, modeling and working with JSON whose structure can vary your first step needs to be to understand the structure of the JSON you want to model, which means you need to go read the documentation from whoever is generating it if it's consistent every time, you'll want to either write or generate C# classes to match if it varies, you will either STILL want to write or generate classes, or you'll want to serialize to a document model and interpret that manually depending on how much variation there is use System.Text.Json unless you have a very specific reason that you can't
Henkypenky
Henkypenky2y ago
did they take that away? there is a way or was edit, paste special, json as classes
JakenVeina
JakenVeina2y ago
within VS itself?
HimmDawg
HimmDawg2y ago
It still exists
JakenVeina
JakenVeina2y ago
news to me definitely doesn't "tie" you into Visual Studio at all, if you did use it
HimmDawg
HimmDawg2y ago
HimmDawg
HimmDawg2y ago
I cant tell how good this is though
Henkypenky
Henkypenky2y ago
yeah, but json2chsarp is better cause records and attributes and etc
JakenVeina
JakenVeina2y ago
so is just writing your own damn code goodthinking
Henkypenky
Henkypenky2y ago
yeah that's fun when you have 3 or 4 kvp's but when you have 400 it becomes tedious
HimmDawg
HimmDawg2y ago
True, but tools tend to fail for big, more complex json
Henkypenky
Henkypenky2y ago
well if you don't check it
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JakenVeina
JakenVeina2y ago
yes, yes, it's been covered
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JakenVeina
JakenVeina2y ago
how dare you
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JakenVeina
JakenVeina2y ago
how dare you
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Daryl
DarylOP2y ago
Oi I'm replying now sorry
JakenVeina
JakenVeina2y ago
funner fact
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Daryl
DarylOP2y ago
Thanks all for the responses. Yes the "Paste Special - JSON as Classes" is what I was referring to but I'm really just trying to understand how others approach working with JSON, regardless of IDE. - In my first project (SpaceTraders.io), I created my first account and was told I'd get an auth token back. I actually got so much JSON that it works out to be 25 classes. One for the ship, one for the crew, one for the fuel, one for the inventory, etc, etc... - In my second project (I just wanted to register my bank transactions to a SQL DB) the Bank's Documentation gave an example of the API Request I'd receive on a transaction but it turns out it's different for different types of transaction it's completely different. My concern is that with these defined classes even if I get it working now, the reliability of my code is in the hands of the SpaceTraders/The Bank because if they make a small change to their API everything I've done could break and be a nightmare to debug. Maybe I was misunderstanding what I read about System.Text.Json not allowing dynamic types. I was trying to get my head around this but it was way too advanced for me at this stage. https://code-maze.com/csharp-deserialize-json-into-dynamic-object/
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JakenVeina
JakenVeina2y ago
the section you're looking for on that article is the third one, concerning the JSON DOM
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JakenVeina
JakenVeina2y ago
the reliability of my code is in the hands of the SpaceTraders/The Bank
Yes, that's a result of the fact that you want to use their system. it's on them to manage changes to their systems in a maintainable way
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JakenVeina
JakenVeina2y ago
or some other scheme of managing changed
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JakenVeina
JakenVeina2y ago
not all changes are breaking and would merit version updates
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JakenVeina
JakenVeina2y ago
ideally
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JakenVeina
JakenVeina2y ago
but there's ultimately no working around the fact that you are beholden to them. That's the peril of building your app on a system that isn't yours.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Daryl
DarylOP2y ago
Yeah ok I get your point, thanks.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Daryl
DarylOP2y ago
In my head I was thinking if it's dynamic at least I get some properties to work on and then I can do null checks? If it's static then there's no room for error?
JakenVeina
JakenVeina2y ago
it's ultimately a judgement call how defensive you want to be but I think we're both telling to tell you that it's not worth worrying this much about it if both of these platforms have PUBLIC APIs, they're likely already documented and versioned to some degree, and you have a pretty high level of trust coding for a little bit of dynamicness in the data is not the same as having to code for every possible variation documentation should be able to lead your way
Daryl
DarylOP2y ago
Yeah no worries, I am over thinking things. Just really caught me off guard when I looked at the response I got and had so much detail in it. Thank you!
Accord
Accord2y 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