0

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 :)

Vaethin
  • 316
  • 4
  • 18
  • Try newtonsoft.json – David Lindon Sep 01 '17 at 10:34
  • Are you trying to send different objects in one request to the API endpoint? – Bert Sinnema Sep 01 '17 at 10:39
  • Have you tried this [C# .net how to deserialize complex object of JSON](https://stackoverflow.com/questions/16339167/c-sharp-net-how-to-deserialize-complex-object-of-json) – ngwanevic Sep 01 '17 at 10:40
  • If I understood your question clearly, you can deserialized your JSON object into a type of object in C# but it will require you to maintain these object classes and, would be vulnerable to issues as well . The best thing to do here is to make it dynamic. You can use C# dynamic functionality in conjunction with JObject and JArray. It's an object inside Json.NET library from Newtonsoft. You can follow this blog as an example on how to use it. https://weblog.west-wind.com/posts/2012/aug/30/using-jsonnet-for-dynamic-json-parsing. Also, try to read and get sample script from Json.NET docus. – Junius Sep 01 '17 at 10:44
  • @Juniuz that seems amazing, going to try it right now! – Vaethin Sep 01 '17 at 12:08

0 Answers0