How I use QuickType to dynamically generate interfaces & classes from JSON APIs?

How I use QuickType to dynamically generate interfaces & classes from JSON APIs?

When working on a project connected to the database, it's easy to generate interfaces and classes from database. You have plenty of tools that can not only generate them but can also create the queries for you.

API

What happens though - when you're consuming data from external APIs - which you don't have access to the properties metadata - only the values themselves? Even if you use Swagger API and can export data, you still need to integrate them in your project, particularly when using typed languages.

One way is to manually create them. However, in real life projects, you have APIs with lots of properties. You need to create dozens of interfaces and/or classes for serializing and deserializing unless you want to use the generic dynamic/object types.

The manual effort is simply not worth the trouble. I once worked on such a task in a TypeScript project with the entity having more than 120 properties. Took quite some time to add in the project, and even had copy and paste issues.

IDE

If you're using IDEs such as Visual Studio & InteliJ Idea, you can generate an interface or class from a JSON file:

Reques.in

This is a good solution, but to use them - usually with typed languages - you will have to deserialize the JSON yourself or include another method. Maybe there is a way to do it faster - I'm still learning the ropes with ASP.NET & Visual Studio IDE.

QuickType

You can use QuickType from a JSON Rest Response. It can generate the classes which is especially relevant for strong typed languages such as C#, Java or TypeScript.

QuickType - Plain Types Only

 

QuickType - All

One feature that I find highly productive is that when you let the default options without plain types only, the classes include date formatting. This in itself is usually a headache.