✅ 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
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'tdid they take that away?
there is a way
or was
edit, paste special, json as classes
within VS itself?
It still exists
news to me
definitely doesn't "tie" you into Visual Studio at all, if you did use it
I cant tell how good this is though
yeah, but json2chsarp is better cause records
and attributes and etc
so is just writing your own damn code
yeah that's fun when you have 3 or 4 kvp's
but when you have 400
it becomes tedious
True, but tools tend to fail for big, more complex json
well if you don't check it
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
yes, yes, it's been covered
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
how dare you
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
how dare you
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Oi I'm replying now sorry
funner fact
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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•2y ago
Message Not Public
Sign In & Join Server To View
the section you're looking for on that article is the third one, concerning the JSON DOM
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
the reliability of my code is in the hands of the SpaceTraders/The BankYes, 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•2y ago
Message Not Public
Sign In & Join Server To View
or some other scheme of managing changed
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
not all changes are breaking and would merit version updates
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
ideally
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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•2y ago
Message Not Public
Sign In & Join Server To View
Yeah ok I get your point, thanks.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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?
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
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!
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.