Java Utililty Methods Json Convert

List of utility methods to do Json Convert

Description

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

Method

ListjsonArrayToBooleanList(final JsonArray arr)
Convert a json array to a list of booleans.
final List<Boolean> out = new ArrayList<>();
if (arr == null)
    return out;
for (int i = 0; i < arr.size(); i++) {
    final Boolean b = arr.getBoolean(i);
    if (b == null) {
        throw new ClassCastException(
                "Element at position " + String.valueOf(i) + " is null - Boolean required");
...
StringjsonArrayToString(final JsonArray array)
Parse a JsonArray into String
final StringBuilder builder = new StringBuilder("[");
final Iterator<JsonValue> iterator = array.iterator();
while (iterator.hasNext()) {
    final JsonValue current = iterator.next();
    if (!builder.toString().equalsIgnoreCase("[")) {
        builder.append(',');
    builder.append(jsonValueToJsonString(current, null));
...
int[]toIntArray(JsonArray jsonArray)
to Int Array
if (jsonArray == null)
    return new int[0];
int[] ret = new int[jsonArray.size()];
for (int i = 0; i < jsonArray.size(); i++) {
    ret[i] = ((JsonNumber) jsonArray.get(i)).intValue();
return ret;
StringtoJsonString(JsonObject json)
Returns a string representation of a json object, or null if json object is null.
return json != null ? json.toString() : null;
ListtoList(JsonArray array)
to List
List<Object> list = new ArrayList<Object>();
for (int i = 0; i < array.size(); i++) {
    Object value = array.get(i);
    if (value instanceof JsonArray) {
        value = toList((JsonArray) value);
    } else if (value instanceof JsonObject) {
        value = toMap((JsonObject) value);
    list.add(value);
return list;
StringtoString(JsonValue json)
Converts a JSON value to an indented text representation.
StringBuilder sb = new StringBuilder();
toString(json, sb, 0);
return sb.toString();
String[]toStringArray(JsonArray jsa)
Convert a JsonArray to a String array.
ArrayList<String> list = new ArrayList<String>();
for (JsonValue v : jsa) {
    if ((v != null) && v.getValueType().equals(ValueType.STRING)) {
        list.add(((JsonString) v).getString());
return list.toArray(new String[0]);