1

I'm working with external APIs, each serves me a very complex json object in response. In order to use it properly on the server, I observe the json structure and create classes according to it. Sometimes I need to generate 20-30 classes to represent the json, and I have dozens of this APIs.

For now, I do this manually for each json.

For example, given the following json:

{
 firstName: "John", 
 lastName: "doe", 
 childrenNames: ["Jean", "Alex"], 
 address: 
 {
  city: "Miami", 
  street: "Abraham Lincoln", 
  house: 314
 }
}

I will first create a class to represent the CityModel, and then the PersonModel, which contains an object from the CityModel class.

Is there any way to automatically create these classes? Maybe IDE plugin (I'm using IntelliJ Idea) or some external tool?

1
  • Have you explored if they external APIs provide a client with the models. Commented Oct 15, 2019 at 23:23

1 Answer 1

3

So, something like that you may have to build out yourself. However, I found that this question has been asked before. And it's been recommend to use the following resource:

http://www.jsonschema2pojo.org/

or go through the headache and build out this feature. Original: Generate Java class from JSON?

Sign up to request clarification or add additional context in comments.

2 Comments

Having the json schema is the key. A sample response may or may not have all the fields and can create incomplete classes.
This answer works perfectly for me. The mentioned website has many options that you can set, preview the result, and get your classes.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.