JSON to Java Class

There are cases where you have JSON data and you want to deal with it in Java. Having to hand code all the classes and their members that represent the data can be very time consuming and error-prone.

I found a very useful website where you can dump the data and gives you the classes. It’s called jsonschema2pojo. The author publishes its source code on GitHub.

For example, I have JSON data that comes back from Jenkins JSON API which lists all the slave nodes. You can get the data from a URL like http://jenkins.hoge.com/computer/api/json If you just copy the data and paste it into jsonschema2pojo, it generates Java classes. For my example, I changed the following settings.

If you click Preview button at the bottom of the screen, you will get Java classes that represent the JSON data. Can you imagine having hand code the following code… And you still have a lot to work on!

-----------------------------------com.hoge.jenkins.lib.AssignedLabel.java-----------------------------------

package com.hoge.jenkins.lib;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"name"
})
public class AssignedLabel {

@JsonProperty("name")
private String name;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("name")
public String getName() {
return name;
}

@JsonProperty("name")
public void setName(String name) {
this.name = name;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.hoge.jenkins.lib.Computer.java-----------------------------------

package com.hoge.jenkins.lib;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"_class",
"actions",
"assignedLabels",
"description",
"displayName",
"executors",
"icon",
"iconClassName",
"idle",
"jnlpAgent",
"launchSupported",
"loadStatistics",
"manualLaunchAllowed",
"monitorData",
"numExecutors",
"offline",
"offlineCause",
"offlineCauseReason",
"oneOffExecutors",
"temporarilyOffline",
"absoluteRemotePath"
})
public class Computer {

@JsonProperty("_class")
private String _class;
@JsonProperty("actions")
private List<Object> actions = null;
@JsonProperty("assignedLabels")
private List<AssignedLabel> assignedLabels = null;
@JsonProperty("description")
private String description;
@JsonProperty("displayName")
private String displayName;
@JsonProperty("executors")
private List<Executor> executors = null;
@JsonProperty("icon")
private String icon;
@JsonProperty("iconClassName")
private String iconClassName;
@JsonProperty("idle")
private Boolean idle;
@JsonProperty("jnlpAgent")
private Boolean jnlpAgent;
@JsonProperty("launchSupported")
private Boolean launchSupported;
@JsonProperty("loadStatistics")
private LoadStatistics loadStatistics;
@JsonProperty("manualLaunchAllowed")
private Boolean manualLaunchAllowed;
@JsonProperty("monitorData")
private MonitorData monitorData;
@JsonProperty("numExecutors")
private Integer numExecutors;
@JsonProperty("offline")
private Boolean offline;
@JsonProperty("offlineCause")
private OfflineCause offlineCause;
@JsonProperty("offlineCauseReason")
private String offlineCauseReason;
@JsonProperty("oneOffExecutors")
private List<Object> oneOffExecutors = null;
@JsonProperty("temporarilyOffline")
private Boolean temporarilyOffline;
@JsonProperty("absoluteRemotePath")
private Object absoluteRemotePath;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("_class")
public String getClass_() {
return _class;
}

@JsonProperty("_class")
public void setClass_(String _class) {
this._class = _class;
}

@JsonProperty("actions")
public List<Object> getActions() {
return actions;
}

@JsonProperty("actions")
public void setActions(List<Object> actions) {
this.actions = actions;
}

@JsonProperty("assignedLabels")
public List<AssignedLabel> getAssignedLabels() {
return assignedLabels;
}

@JsonProperty("assignedLabels")
public void setAssignedLabels(List<AssignedLabel> assignedLabels) {
this.assignedLabels = assignedLabels;
}

@JsonProperty("description")
public String getDescription() {
return description;
}

@JsonProperty("description")
public void setDescription(String description) {
this.description = description;
}

@JsonProperty("displayName")
public String getDisplayName() {
return displayName;
}

@JsonProperty("displayName")
public void setDisplayName(String displayName) {
this.displayName = displayName;
}

@JsonProperty("executors")
public List<Executor> getExecutors() {
return executors;
}

@JsonProperty("executors")
public void setExecutors(List<Executor> executors) {
this.executors = executors;
}

@JsonProperty("icon")
public String getIcon() {
return icon;
}

@JsonProperty("icon")
public void setIcon(String icon) {
this.icon = icon;
}

@JsonProperty("iconClassName")
public String getIconClassName() {
return iconClassName;
}

@JsonProperty("iconClassName")
public void setIconClassName(String iconClassName) {
this.iconClassName = iconClassName;
}

@JsonProperty("idle")
public Boolean getIdle() {
return idle;
}

@JsonProperty("idle")
public void setIdle(Boolean idle) {
this.idle = idle;
}

@JsonProperty("jnlpAgent")
public Boolean getJnlpAgent() {
return jnlpAgent;
}

@JsonProperty("jnlpAgent")
public void setJnlpAgent(Boolean jnlpAgent) {
this.jnlpAgent = jnlpAgent;
}

@JsonProperty("launchSupported")
public Boolean getLaunchSupported() {
return launchSupported;
}

@JsonProperty("launchSupported")
public void setLaunchSupported(Boolean launchSupported) {
this.launchSupported = launchSupported;
}

@JsonProperty("loadStatistics")
public LoadStatistics getLoadStatistics() {
return loadStatistics;
}

@JsonProperty("loadStatistics")
public void setLoadStatistics(LoadStatistics loadStatistics) {
this.loadStatistics = loadStatistics;
}

@JsonProperty("manualLaunchAllowed")
public Boolean getManualLaunchAllowed() {
return manualLaunchAllowed;
}

@JsonProperty("manualLaunchAllowed")
public void setManualLaunchAllowed(Boolean manualLaunchAllowed) {
this.manualLaunchAllowed = manualLaunchAllowed;
}

@JsonProperty("monitorData")
public MonitorData getMonitorData() {
return monitorData;
}

@JsonProperty("monitorData")
public void setMonitorData(MonitorData monitorData) {
this.monitorData = monitorData;
}

@JsonProperty("numExecutors")
public Integer getNumExecutors() {
return numExecutors;
}

@JsonProperty("numExecutors")
public void setNumExecutors(Integer numExecutors) {
this.numExecutors = numExecutors;
}

@JsonProperty("offline")
public Boolean getOffline() {
return offline;
}

@JsonProperty("offline")
public void setOffline(Boolean offline) {
this.offline = offline;
}

@JsonProperty("offlineCause")
public OfflineCause getOfflineCause() {
return offlineCause;
}

@JsonProperty("offlineCause")
public void setOfflineCause(OfflineCause offlineCause) {
this.offlineCause = offlineCause;
}

@JsonProperty("offlineCauseReason")
public String getOfflineCauseReason() {
return offlineCauseReason;
}

@JsonProperty("offlineCauseReason")
public void setOfflineCauseReason(String offlineCauseReason) {
this.offlineCauseReason = offlineCauseReason;
}

@JsonProperty("oneOffExecutors")
public List<Object> getOneOffExecutors() {
return oneOffExecutors;
}

@JsonProperty("oneOffExecutors")
public void setOneOffExecutors(List<Object> oneOffExecutors) {
this.oneOffExecutors = oneOffExecutors;
}

@JsonProperty("temporarilyOffline")
public Boolean getTemporarilyOffline() {
return temporarilyOffline;
}

@JsonProperty("temporarilyOffline")
public void setTemporarilyOffline(Boolean temporarilyOffline) {
this.temporarilyOffline = temporarilyOffline;
}

@JsonProperty("absoluteRemotePath")
public Object getAbsoluteRemotePath() {
return absoluteRemotePath;
}

@JsonProperty("absoluteRemotePath")
public void setAbsoluteRemotePath(Object absoluteRemotePath) {
this.absoluteRemotePath = absoluteRemotePath;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.hoge.jenkins.lib.ComputerCollection.java-----------------------------------

package com.hoge.jenkins.lib;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"_class",
"busyExecutors",
"computer",
"displayName",
"totalExecutors"
})
public class ComputerCollection {

@JsonProperty("_class")
private String _class;
@JsonProperty("busyExecutors")
private Integer busyExecutors;
@JsonProperty("computer")
private List<Computer> computer = null;
@JsonProperty("displayName")
private String displayName;
@JsonProperty("totalExecutors")
private Integer totalExecutors;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("_class")
public String getClass_() {
return _class;
}

@JsonProperty("_class")
public void setClass_(String _class) {
this._class = _class;
}

@JsonProperty("busyExecutors")
public Integer getBusyExecutors() {
return busyExecutors;
}

@JsonProperty("busyExecutors")
public void setBusyExecutors(Integer busyExecutors) {
this.busyExecutors = busyExecutors;
}

@JsonProperty("computer")
public List<Computer> getComputer() {
return computer;
}

@JsonProperty("computer")
public void setComputer(List<Computer> computer) {
this.computer = computer;
}

@JsonProperty("displayName")
public String getDisplayName() {
return displayName;
}

@JsonProperty("displayName")
public void setDisplayName(String displayName) {
this.displayName = displayName;
}

@JsonProperty("totalExecutors")
public Integer getTotalExecutors() {
return totalExecutors;
}

@JsonProperty("totalExecutors")
public void setTotalExecutors(Integer totalExecutors) {
this.totalExecutors = totalExecutors;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.hoge.jenkins.lib.Executor.java-----------------------------------

package com.hoge.jenkins.lib;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({

})
public class Executor {

@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.hoge.jenkins.lib.LoadStatistics.java-----------------------------------

package com.hoge.jenkins.lib;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"_class"
})
public class LoadStatistics {

@JsonProperty("_class")
private String _class;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("_class")
public String getClass_() {
return _class;
}

@JsonProperty("_class")
public void setClass_(String _class) {
this._class = _class;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.hoge.jenkins.lib.MonitorData.java-----------------------------------

package com.hoge.jenkins.lib;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"hudson.node_monitors.SwapSpaceMonitor",
"hudson.node_monitors.TemporarySpaceMonitor",
"hudson.node_monitors.DiskSpaceMonitor",
"hudson.node_monitors.ArchitectureMonitor",
"hudson.node_monitors.ResponseTimeMonitor",
"hudson.node_monitors.ClockMonitor"
})
public class MonitorData {

@JsonProperty("hudson.node_monitors.SwapSpaceMonitor")
private Object hudsonNodeMonitorsSwapSpaceMonitor;
@JsonProperty("hudson.node_monitors.TemporarySpaceMonitor")
private Object hudsonNodeMonitorsTemporarySpaceMonitor;
@JsonProperty("hudson.node_monitors.DiskSpaceMonitor")
private Object hudsonNodeMonitorsDiskSpaceMonitor;
@JsonProperty("hudson.node_monitors.ArchitectureMonitor")
private Object hudsonNodeMonitorsArchitectureMonitor;
@JsonProperty("hudson.node_monitors.ResponseTimeMonitor")
private Object hudsonNodeMonitorsResponseTimeMonitor;
@JsonProperty("hudson.node_monitors.ClockMonitor")
private Object hudsonNodeMonitorsClockMonitor;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("hudson.node_monitors.SwapSpaceMonitor")
public Object getHudsonNodeMonitorsSwapSpaceMonitor() {
return hudsonNodeMonitorsSwapSpaceMonitor;
}

@JsonProperty("hudson.node_monitors.SwapSpaceMonitor")
public void setHudsonNodeMonitorsSwapSpaceMonitor(Object hudsonNodeMonitorsSwapSpaceMonitor) {
this.hudsonNodeMonitorsSwapSpaceMonitor = hudsonNodeMonitorsSwapSpaceMonitor;
}

@JsonProperty("hudson.node_monitors.TemporarySpaceMonitor")
public Object getHudsonNodeMonitorsTemporarySpaceMonitor() {
return hudsonNodeMonitorsTemporarySpaceMonitor;
}

@JsonProperty("hudson.node_monitors.TemporarySpaceMonitor")
public void setHudsonNodeMonitorsTemporarySpaceMonitor(Object hudsonNodeMonitorsTemporarySpaceMonitor) {
this.hudsonNodeMonitorsTemporarySpaceMonitor = hudsonNodeMonitorsTemporarySpaceMonitor;
}

@JsonProperty("hudson.node_monitors.DiskSpaceMonitor")
public Object getHudsonNodeMonitorsDiskSpaceMonitor() {
return hudsonNodeMonitorsDiskSpaceMonitor;
}

@JsonProperty("hudson.node_monitors.DiskSpaceMonitor")
public void setHudsonNodeMonitorsDiskSpaceMonitor(Object hudsonNodeMonitorsDiskSpaceMonitor) {
this.hudsonNodeMonitorsDiskSpaceMonitor = hudsonNodeMonitorsDiskSpaceMonitor;
}

@JsonProperty("hudson.node_monitors.ArchitectureMonitor")
public Object getHudsonNodeMonitorsArchitectureMonitor() {
return hudsonNodeMonitorsArchitectureMonitor;
}

@JsonProperty("hudson.node_monitors.ArchitectureMonitor")
public void setHudsonNodeMonitorsArchitectureMonitor(Object hudsonNodeMonitorsArchitectureMonitor) {
this.hudsonNodeMonitorsArchitectureMonitor = hudsonNodeMonitorsArchitectureMonitor;
}

@JsonProperty("hudson.node_monitors.ResponseTimeMonitor")
public Object getHudsonNodeMonitorsResponseTimeMonitor() {
return hudsonNodeMonitorsResponseTimeMonitor;
}

@JsonProperty("hudson.node_monitors.ResponseTimeMonitor")
public void setHudsonNodeMonitorsResponseTimeMonitor(Object hudsonNodeMonitorsResponseTimeMonitor) {
this.hudsonNodeMonitorsResponseTimeMonitor = hudsonNodeMonitorsResponseTimeMonitor;
}

@JsonProperty("hudson.node_monitors.ClockMonitor")
public Object getHudsonNodeMonitorsClockMonitor() {
return hudsonNodeMonitorsClockMonitor;
}

@JsonProperty("hudson.node_monitors.ClockMonitor")
public void setHudsonNodeMonitorsClockMonitor(Object hudsonNodeMonitorsClockMonitor) {
this.hudsonNodeMonitorsClockMonitor = hudsonNodeMonitorsClockMonitor;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.hoge.jenkins.lib.OfflineCause.java-----------------------------------

package com.hoge.jenkins.lib;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"_class"
})
public class OfflineCause {

@JsonProperty("_class")
private String _class;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("_class")
public String getClass_() {
return _class;
}

@JsonProperty("_class")
public void setClass_(String _class) {
this._class = _class;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

I will write how you can use the generated classes with the JSON data in my later blog posts.

Author: admin

A software engineer in greater Seattle area

Leave a Reply

Your email address will not be published. Required fields are marked *