skip to Main Content

**I want to convert JSON into Java Pojo dynamically , meaning JSON structure will be change but I need to convert that JSON into Java Pojo without creating JSON POJO, is there any framework available ?

I thought about Dozzer mapper but in dozzer mapper I always need to create SRC Object POJO ,I need to avoid same.
**

2

Answers


  1. Your question is too general. But here are few scenarios.

    1. Assume that your JSON will always have n properties and may have or not have other properties that are not relevant to you. In this case, if you create a POJO that contains your n properties and use the JACKSON library you can mark your POJO with @JsonIgnoreProperties(ignoreUnknown = true) annotation and the other properties in your JSON could be ignored
    2. If you really receive a JSON object with the completely unknown structure you can only convert it into a Map<String, Object> that would be your only option.

    While you can use for your conversions Json-Jackson library or Gson library, I actually wrote my own wrapper over Jackson ObjectMapper class that may simplify this task for you. This feature comes as part of MgntUtils Open-Source java library written and maintained by me. Your code may look like:

    Map<String, Object> map = JsonUtils.readObjectFromJsonString(jsonString, Map.class);
    

    It throws IOException though so you will need to handle it. Here is the Javadoc. The MgntUtils library could be found as Maven artifact and on the Github (including javadoc and source code)

    Login or Signup to reply.
  2. You can parse your json into a lightweight Json generic object provided by one of the Json libraries available that provide such a direct approach.

    For instance essential-json (disclaimer – I’m the author), json-simple, json-java, …

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search