Posted In: Jackson 2X, JSON

Generate Java POJO from JSON or JSON schema

 
This example will show how to generate JOSN schema from JSON. It will also show how to generate Java POJO either from JSON schema or JSON
 

1. Example will use following sample JSON

{
  "addresses": [
    {
      "addressID": "90ace06b-2257-4760-9dec-75a4ca679d0e",
      "creationDateTime": "2014-08-19T22:04:56.814-07:00",
      "lastUpdDateTime": "2014-08-19T22:04:56.814-07:00",
      "country": "India",
      "state": "Telengana",
      "pinCode": "400066",
      "streetName": "street",
      "buildingName": "my buliding",
      "flatNumber": "102"
    },
    {
      "addressID": "90ace06b-2257-4760-9dec-75a4ca679d0e",
      "creationDateTime": "2014-08-19T22:04:56.814-07:00",
      "lastUpdDateTime": "2014-08-19T22:04:56.814-07:00",
      "country": "India",
      "state": "Telengana",
      "pinCode": "400066",
      "streetName": "street",
      "buildingName": "my buliding",
      "flatNumber": "102"
    }
  ]
}

 
 

2. To format JSON properly online JSON editors are available. You could use www.jsoneditoronline.org

 

 
 

3. To generate schema from JSON use jsonschema.net

 

 

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "addresses": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "addressID": {
            "type": "string"
          },
          "creationDateTime": {
            "type": "string"
          },
          "lastUpdDateTime": {
            "type": "string"
          },
          "country": {
            "type": "string"
          },
          "state": {
            "type": "string"
          },
          "pinCode": {
            "type": "string"
          },
          "streetName": {
            "type": "string"
          },
          "buildingName": {
            "type": "string"
          },
          "flatNumber": {
            "type": "string"
          }
        },
        "required": [
          "addressID",
          "creationDateTime",
          "lastUpdDateTime",
          "country",
          "state",
          "pinCode",
          "streetName",
          "buildingName",
          "flatNumber"
        ]
      }
    }
  },
  "required": [
    "addresses"
  ]
}

 
 

4a. Generate POJO classes using JSON schema or JSON. www.jsonschema2pojo.org

 

 
 

4b. Following POJOs will get generated. Download generated ZIP

 

package com.example;

import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.commons.lang3.builder.ToStringBuilder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "addresses"
})
public class Addresses {

    @JsonProperty("addresses")
    private List<Address> addresses = null;
    
    @JsonProperty("addresses")
    public List<Address> getAddresses() {
        return addresses;
    }
    @JsonProperty("addresses")
    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}

 

package com.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.commons.lang3.builder.ToStringBuilder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "addressID",
    "creationDateTime",
    "lastUpdDateTime",
    "country",
    "state",
    "pinCode",
    "streetName",
    "buildingName",
    "flatNumber"
})
public class Address {
    @JsonProperty("addressID")
    private String addressID;
    @JsonProperty("creationDateTime")
    private String creationDateTime;
    @JsonProperty("lastUpdDateTime")
    private String lastUpdDateTime;
    @JsonProperty("country")
    private String country;
    @JsonProperty("state")
    private String state;
    @JsonProperty("pinCode")
    private String pinCode;
    @JsonProperty("streetName")
    private String streetName;
    @JsonProperty("buildingName")
    private String buildingName;
    @JsonProperty("flatNumber")
    private String flatNumber;
    @JsonProperty("addressID")
    public String getAddressID() {
        return addressID;
    }
    @JsonProperty("addressID")
    public void setAddressID(String addressID) {
        this.addressID = addressID;
    }
    @JsonProperty("creationDateTime")
    public String getCreationDateTime() {
        return creationDateTime;
    }
    @JsonProperty("creationDateTime")
    public void setCreationDateTime(String creationDateTime) {
        this.creationDateTime = creationDateTime;
    }
    @JsonProperty("lastUpdDateTime")
    public String getLastUpdDateTime() {
        return lastUpdDateTime;
    }
    @JsonProperty("lastUpdDateTime")
    public void setLastUpdDateTime(String lastUpdDateTime) {
        this.lastUpdDateTime = lastUpdDateTime;
    }
    @JsonProperty("country")
    public String getCountry() {
        return country;
    }
    @JsonProperty("country")
    public void setCountry(String country) {
        this.country = country;
    }
    @JsonProperty("state")
    public String getState() {
        return state;
    }
    @JsonProperty("state")
    public void setState(String state) {
        this.state = state;
    }
    @JsonProperty("pinCode")
    public String getPinCode() {
        return pinCode;
    }
    @JsonProperty("pinCode")
    public void setPinCode(String pinCode) {
        this.pinCode = pinCode;
    }
    @JsonProperty("streetName")
    public String getStreetName() {
        return streetName;
    }
    @JsonProperty("streetName")
    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }
    @JsonProperty("buildingName")
    public String getBuildingName() {
        return buildingName;
    }
    @JsonProperty("buildingName")
    public void setBuildingName(String buildingName) {
        this.buildingName = buildingName;
    }
    @JsonProperty("flatNumber")
    public String getFlatNumber() {
        return flatNumber;
    }
    @JsonProperty("flatNumber")
    public void setFlatNumber(String flatNumber) {
        this.flatNumber = flatNumber;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}

 
 

5. Now use Jackson Object Mapper to create Addresses Object by parsing JSON

 

package com.example;

import java.io.Reader;
import java.io.StringReader;

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
	public static void main(String a[]) {
		ObjectMapper objectMapper = new ObjectMapper();
		String json =
"{"+
  "\"addresses\": ["+
    "{"+
     " \"addressID\": \"90ace06b-2257-4760-9dec-75a4ca679d0e\","+
     " \"creationDateTime\": \"2014-08-19T22:04:56.814-07:00\","+
     " \"lastUpdDateTime\": \"2014-08-19T22:04:56.814-07:00\","+
     " \"country\": \"India\","+
     " \"state\": \"Telengana\","+
     " \"pinCode\": \"400066\","+
     " \"streetName\": \"street\","+
     " \"buildingName\": \"my buliding\","+
     " \"flatNumber\": \"102\""+
    "},"+
    "{"+
    "  \"addressID\": \"90ace06b-2257-4760-9dec-75a4ca679d0e\","+
    "  \"creationDateTime\": \"2014-08-19T22:04:56.814-07:00\","+
    "  \"lastUpdDateTime\": \"2014-08-19T22:04:56.814-07:00\","+
    "  \"country\": \"India\","+
    "  \"state\": \"Telengana\","+
    "  \"pinCode\": \"400066\","+
    "  \"streetName\": \"street\","+
    "  \"buildingName\": \"my buliding\","+
    "  \"flatNumber\": \"102\""+
    "}"+
  "]"+
"}";
		try {
			Reader reader = new StringReader(json);
			Addresses addresses = objectMapper.readValue(reader,
			        Addresses.class);
			System.out.println(addresses);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 
 

Tags: , ,

by , on February 20th, 2017

  • Categories