1

I'm trying to build a JSON object based on the ResultSet(s) returned by our RDBMS. Each row in the ResultSet represents one object that we want to add to an array as part of the JSON. We've got the JSON schema defined in another file and I'm trying to figure out how to build the JSON based on the predefined schema.

I'm not sure if this is the best approach, to be honest. When first looking into how to approach this I was looking at JsonObjectBuilder, but with the number of fields involved it would get pretty messy.

I'm thinking my best bet might be to create a POJO to hold the data from the ResultSet and then use Jackson ObjectMapper to convert it into the desired JSON format.

In short:

  1. How would I dynamically create a JSON object based on a schema and populated with the results of a database query?
  2. If that's not feasible or efficient, what would be a better/the best way to dynamically create a JSON object populated with the results of a database query and adhering to a specific format?
3
  • What exactly do you need JSON for? If I understand the question correctly, a serializable POJO would work Commented May 2, 2016 at 16:14
  • The JSON will be POSTed to another endpoint for processing/insertion into a different database. That's why it has to adhere to a specific format. Commented May 2, 2016 at 16:19
  • 1
    And I'm guessing you have to stick with an RDBMS? If so, then DB->POJO->JSON is the best option you have. Commented May 2, 2016 at 16:25

1 Answer 1

1

Consider getting out of the JDBC game. MyBatis and Hibernate (probably other libs as well) do a much better job than you will ever do with a roll-your-own solution. I suggest MyBatis for simple stuff and Hibernate if you need features that are not in MyBatis.

With that in mind. create a POJO for the results of a MyBatis query then use Jackson (or gson or some other JSON library) to create the JSON.

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

Comments

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.