2

So I know a little bit about parsing Json data but not too much so pardon if I am not describing everything as I should. Lets use this Json file as an example:

{  
  "firstname": "John",  
  "lastname": "Smith",  
  "age": 25,  
  "address": {  
    "street address": "21 2nd Street",  
    "city": "New York",  
    "state": "NY",  
    "postal code": "10021"  
  },  
  "phonenumbers": [  
    {  
      "type": "home",  
      "number": "212 555-1234"  
    },  
    {  
      "type": "fax",  
      "number": "646 555-4567"  
    }  
  ],  
  "sex": {  
    "type": "male"  
  }  
}  

I know that if I want to parse this json data I will first have to create a class with these names in script (I didnt write them all out I know)

public class JsonClass
{
    string firstname;
    List<phonenumberlist> phonenumbers;
}

public class phonenumberlist
{
    string type;
    string number;
}

but my question is what if I want to dynamically change the json file and how can I go about setting up new classes dynamically in accordance to my dynamically changing json file? for example If i add to the Json file under phone numbers list.

"areaCodeLocation": "new york"

Then I wouldnt be able to parse it since I would have to manually go into my code and update the class I had written. Or what if I wanted to add something else? like
"height" : "6"

Basically what Im asking is how to dynamically build a parser to change according to my dynamically changing json, if that even is possible? Thanks!

Edit: To further clarify what I am trying to achieve. I will host a json file on the internet and I will parse it when my program starts. This is not a problem normally because everything is properly initialized written through classes in the code. But In the event I decide to add more information to my json file like "areaCodeLocation" or "height" as shown above = this will not be parsed because It is not initialized in the class.

What I want to do, if possible, is for the json file to be parsed through code, including setting up the classes, which will allow the program to parse any additional information I may want to add to the json file (hosted online) without myself going in to my code and manually adding new classes for parsing. This is so my users will not have to update the program every time I decide to add something new to the json file since it will be done within the code.

7
  • 1
    do you mean you want to parse to a dynamic object, or dynamcaly generate a static object?
    – Ewan
    Commented Jan 20, 2020 at 9:33
  • 1
    How much "dynamic" are you talking about here? Every second, every minute, every hour, every day, every week, every month?
    – Euphoric
    Commented Jan 20, 2020 at 10:48
  • 2
    What's the point in dynamically changing the JSON file if you don't write code to handle the new data? And if you do, why can't you change the class too? Commented Jan 20, 2020 at 13:35
  • @Sebastian Redl There may be stuff I forget to add or there may be more information I want to add to the JSON file at a later point in time that is not written in the class. I cant change the class cuz I dont want my users to have to update the software everytime I decide to add something to the JSON data. The JSON data is hosted online and then it will be parsed on initialization by the program. Commented Jan 21, 2020 at 1:20
  • 1
    Most JSON deserializers (like Json.Net) ignore (or can be configured to) fields in the JSON that don't map to any class members. Just because you add stuff to the JSON doesn't mean you have to update your types. Commented Jan 21, 2020 at 9:13

1 Answer 1

1

Pardon me if I've misunderstood your question, but the way I see it Newtonsoft.Json library has JObject class which actually represents dynamic JSON piece. Here's the snippet code which shows how you can query it to get only the properties you're interested in your business logic.

3
  • @Bodhan Stupak Perhaps I worded the question strangely but I am going to host a JSON file online and in my program I want to parse it when the program starts, Normally this is not an issue but in the case I want to change my JSON file with new data, data that is NOT in the class, I dont know how to do that other than going in and manually adding more to the class or a new class - which is something I dont want to do, which is why I want to know if theres a way to parse it without me going in there and changing the class myself. I dont want to have to update the program just cuz i added new jsn Commented Jan 21, 2020 at 1:32
  • 2
    @EmperiumIce But he answered your question. There are many ways to parse data that's not "in your class". Your class could contain a reference t the JObject, You could use a structure of Maps and Lists to represent uncontrolled areas of the json. You can decide how much extension or variation to allow.
    – joshp
    Commented Jan 21, 2020 at 2:49
  • @joshp oh i see, im dumb Commented Jan 21, 2020 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.