Java Utililty Methods Json Create

List of utility methods to do Json Create

Description

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

Method

JsonObjectbuild(Map map)
Builds a JsonObject from an arbitrarily nested Map.
JsonObjectBuilder root = Json.createObjectBuilder();
for (Object key : map.keySet()) {
    Object value = map.get(key);
    if (value == null) {
        root.addNull(key.toString());
    } else if (value instanceof Map) {
        root.add(key.toString(), build((Map) value));
    } else {
...
JsonObjectbuildContentResponse(String userId, String message)
build Content Response
JsonObjectBuilder content = Json.createObjectBuilder();
content.add(userId, message);
return content.build();
JsonArraybuildJsonArray(String[][] twoDimensionArray)
Builds two dimensional JsonArray form String[][]
JsonArrayBuilder keyboardBuilder = Json.createArrayBuilder();
for (String[] boardElement : twoDimensionArray) {
    JsonArrayBuilder key = Json.createArrayBuilder();
    for (String keyElement : boardElement) {
        key.add(keyElement);
    keyboardBuilder.add(key.build());
return keyboardBuilder.build();
StringconvertToJson(Object myObject)
convert To Json
if (myObject instanceof Map<?, ?>) {
    final Map<?, ?> map = (Map<?, ?>) myObject;
    final StringBuilder sb = new StringBuilder("{");
    boolean first = true;
    for (final Map.Entry<?, ?> entry : map.entrySet()) {
        if (first) {
            first = false;
        } else {
...
StringconvertToJson(Object obj)
Convert a nested Map data structure to a JSON String.
return convertToJson(obj, false);
JsonArrayBuildercreateArrayBuilder()
create Array Builder
return PROVIDER.createArrayBuilder();
JsonArrayBuildercreateArrayBuilder()
Creates a JSON array builder
return createJsonProvider().createArrayBuilder();
JsonObjectBuildercreateJsonFrom(JsonObject user, String... ignoreKeys)
create Json From
JsonObjectBuilder builder = Json.createObjectBuilder();
List<String> doNotCopy = Arrays.asList(ignoreKeys);
for (Map.Entry<String, JsonValue> entry : user.entrySet()) {
    if (!!!doNotCopy.contains(entry.getKey())) {
        builder.add(entry.getKey(), entry.getValue());
return builder;
...
JsonValuecreateJsonValue(String string)
Create a JsonValue using a String
return Json.createObjectBuilder().add("value", string).build().getJsonString("value");
JsonWriterFactorycreatePrettyWriterFactory()
create Pretty Writer Factory
HashMap<String, Object> prettyConfigMap = new HashMap<String, Object>();
prettyConfigMap.put(JsonGenerator.PRETTY_PRINTING, Boolean.TRUE);
JsonWriterFactory prettyWriterFactory = Json.createWriterFactory(prettyConfigMap);
return prettyWriterFactory;