Java Utililty Methods Json

List of utility methods to do Json

Description

The list of methods to do Json are organized into topic(s).

Method

JSONArraygetAsJSONArray(Object obj)
This method takes an object that is JSONArray already or JSONObject and convert it into JSONArray this is useful because JSON turn an xml entry into array or object according to number of entries using this method will avoid to treat as special cases whether you have one entrie or several.
JSONArray result = null;
if (obj instanceof JSONArray) {
    result = (JSONArray) obj;
} else if (obj instanceof JSONObject) {
    result = new JSONArray();
    result.put(obj);
} else {
    throw new ParseException("problem while interpreting " + obj, 0);
...
TgetFromGson(String json, Class clazz)
get From Gson
return G.fromJson(json, clazz);
StringgetJSONDate(long l)
get JSON Date
return getDateString(l, "yyyy-MM-dd'T'HH:mm:ss");
ObjectMappergetJsonMapper()
get Json Mapper
return mapper.get();
booleanhasKey(JsonObject object, String name)
has Key
JsonValue value = object.get(name);
if ((value == null) || (value == JsonValue.NULL)) {
    return false;
return true;
booleanjsonEquals(JsonArray expected, JsonArray actual, boolean strict)
json Equals
return compare(expected, actual, strict);
JsonObjectjsonObject(String key, JsonValue val)
json Object
return Json.createObjectBuilder().add(key, val).build();
JsonObjectBuilderjsonObjectToBuilder(JsonObject jo)
json Object To Builder
JsonObjectBuilder job = Json.createObjectBuilder();
for (Entry<String, JsonValue> entry : jo.entrySet()) {
    job.add(entry.getKey(), entry.getValue());
return job;
voidjsonPrettyPrinter(Object object)
This printer only support JsonObject and JsonArray
if (!(object instanceof JsonObject || object instanceof JsonArray)) {
    System.out.println(object.toString());
    return;
String newLine = System.getProperty("line.separator");
StringBuilder strb = new StringBuilder();
char[] charArr = object.toString().toCharArray();
int depth = 0;
...
StringjsonString(JsonValue value)
json String
if (value == null || value.getValueType() == JsonValue.ValueType.NULL)
    return "";
if (value.getValueType() == JsonValue.ValueType.STRING && (value instanceof JsonString))
    return ((JsonString) value).getString();
return "";