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

StringjsonValueToJsonString(final JsonValue jsonValue, final String key)
Parse a JsonValue into String
final StringBuilder builder = new StringBuilder();
if (key != null) {
    builder.append('\"').append(key).append("\":");
if (jsonValue instanceof JsonString) {
    builder.append('\"').append(jsonValue.toString()).append('\"');
} else if (jsonValue instanceof JsonNumber || jsonValue instanceof JsonObject) {
    builder.append(jsonValue.toString());
...
JsonArraylistToJsonArray(List list)
Method used to build the JSON Array of Strings.
JsonArrayBuilder ab = Json.createArrayBuilder();
for (String element : list) {
    ab.add(element);
return ab.build();
JsonObjectBuildermapToJsonObjectBuilder(Map map)
map To Json Object Builder
JsonObjectBuilder jsonObject = Json.createObjectBuilder();
if (map == null || map.isEmpty()) {
    return jsonObject;
Iterator<Entry<String, String>> i = map.entrySet().iterator();
while (i.hasNext()) {
    Entry<String, String> e = i.next();
    jsonObject.add(e.getKey(), e.getValue());
...
JsonObjectnewObject()
Creates a new object.
return Json.createObjectBuilder().build();
JsonValuenullable(String value)
Returns a JsonValue from a String.
if (value == null) {
    return JsonValue.NULL;
return Json.createObjectBuilder().add("_", value).build().get("_");
StringobjectToJsonDateSerializer(Object ts, final String dateformat)
object To Json Date Serializer
String jsonStr = null;
gson = new GsonBuilder().registerTypeHierarchyAdapter(Date.class, new JsonSerializer<Date>() {
    public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
        SimpleDateFormat format = new SimpleDateFormat(dateformat);
        return new JsonPrimitive(format.format(src));
}).setDateFormat(dateformat).create();
if (gson != null) {
...
voidprintJavaxJsonProvider()
print Javax Json Provider
System.out.println("Jsonp Provider:" + JsonProvider.provider().getClass().getCanonicalName());
System.out.println();
Objectread(String jsonString)
read
return parse(new StringCharacterIterator(jsonString));
DatereadMandatoryDateField(JsonParser jsonParser, String fieldName)
read Mandatory Date Field
String value = readMandatoryField(jsonParser, fieldName);
if (value == null) {
    return null;
return parseDateValue(value);
booleanrequiredParams(JsonObject map, String... params)
Checks if the parameters specified (which are required) are used as keys in the specified map
for (String param : params) {
    if (!map.containsKey(param)) {
        return false;
return true;