I am making a request to an API that returns information about a bunch of objects to me as a JSON string, similar to this: (handwritten so ignore possible typos)
{{
"count": 2,
"object": [{
"name": "object 1",
"fields": {
"Attribute1": 2,
"Attribute2": "string",
(...)}
"links": [{
"rel": "SomeString",
"url": "http://UrlString"
},
{
...
}]
}],
[{
"name": "object 2",
"fields": {
"Attribute1": 3,
"Attribute4": 5,
"Attribute6": "foo",
(...)
I turned this into a class by making a sample request and using "Insert JSON as class" in Visual Studio but, due to the fact that different Objects have different fields (API won't return a value when it's Null but I don't have a complete list of all possible fields), the class is already a 135 lines long and that was with a very basic request.
Also I am worried what might happen when I do happen to get a result that has a field that isn't specified in the class. For all I know it might cause an Exception or simply ignore everything that isn't explicitly specified.
Is there a way to work with these objects (I am trying to save them in an Azure SQL Server) without losing any information?
I was thinking about KeyValuePairs but I don't think that will work with var (because I don't know if the value is string or integer). Also it will make it awkward to write to SQL because every time I "discover" a new field the whole db would have to be re-created, but since Azure SQL Servers have JSON Support this might be easier than it sounds.
I would be perfectly happy with getting the "name" field, making it a field in the db and then storing the "fields" and the "links" field directly as a JSON string in the db but I need to access some of the fields during computation so I'd have to convert from Response to Object back to string. If I don't have every field in my class I am again worried that I'll lose something.
Sorry for the long text, hope I'm not seeing something here :)