'How do i convert this complex json to dart model class with null safety
Here is my json file which I want to convert to dart model
{
"data": {
"catalog_id": "615ac5699a3c9f2ea3a65180",
"catalog_images": {
"l_large": {
"url": ""
},
"l_medium": {
"url": ""
},
"p_small": {
"url": "https://s3-ap-southeast-1.amazonaws.com/ott-as-service/ott_default_images/default.png"
}
},
"videolist_tags2": [],
"items": [
{
"title": "HERE AND NOW",
"content_id": "615acc129a3c9f2ea3a6518c",
"status": "published",
"sequence_no": 1,
"catalog_id": "615ac5699a3c9f2ea3a65180",
"catalog_object": {
"friendly_id": "movies",
"layout_type": "t_16_9_big_meta",
"id": "615ac5699a3c9f2ea3a65180",
"plan_category_type": "",
"layout_scheme": "",
"catalog_id": "615ac5699a3c9f2ea3a65180"
},
"play_url": {
"saranyu": {
"url": "http://52.77.63.32//v2/smart_urls/61c5c5868530b8bb03e2b625"
}
},
....
}
}
I would like a tool which can auto generate a model for me, because it is very time consuming manually.
Solution 1:[1]
You can use this tool to convert your json to dart. It also supports null safety and complex lists.
Solution 2:[2]
just go to browser and type quick type that is the best for converting json to any model class.copy your json code and paste it over there it will generate your model class.
Solution 3:[3]
There is no way to do that perfectly. JSON does not contain type information of the kind you need for null safety.
For example, there is no way to know, whether there are fields that are nullable, because it would be perfectly fine to just not have them in the JSON data at all. There also is no way to know whether a field is nullable, that right now is in there and has a value.
So, you can use a generator, but to actually create a good model, you will either need your own knowledge or you need a description like Swagger/OpenAPI that is not just example data, but actual interface definition.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Muhammad Hussain |
Solution 2 | appdev |
Solution 3 | nvoigt |