Map a String into JSON using JsonGenerator

1.3k Views Asked by At

Given is a JSON array

[
    {
        "ref": "idValue",
        "data": {
            "filter": [
                {
                    "property": "number"
                }
            ],
            "stateId": "contacts"
        }
    }
]

I store this array as String in a database.

Now I want to embed the given String into a new JSON object, generated dynamically by javax.json.stream.JsonGenerator.

A possible result should look like this:

{
    "newValue": "someValue",
    "storedFilters": [
        {
            "ref": "idValue",
            "data": {
                "filter": [
                    {
                        "property": "number"
                    }
                ],
                "stateId": "contacts"
            }
        }
    ]
}

Is there a possibility to achieve this?

My approach:

String jsonStringValue = ... // Contains JSON content of a previous request
HttpServletResponse resp = ...
List<Event> events = ...
OutputStream os = resp.getOutputStream();
Map<String, Boolean> config = new HashMap<String, Boolean>();
if (prettyPrint) {
    config.put(JsonGenerator.PRETTY_PRINTING, Boolean.TRUE);
}
JsonGeneratorFactory jgf = Json.createGeneratorFactory(config);
JsonGenerator jg = jgf.createGenerator(os);

if (events.size() > 1) {
    jg.writeStartArray();
}

// Some generic stuff happens here which is based on the 'events' list

jg.writeStartObject();
jg.write(JsonConstants.FILTERS, jsonStringValue);
jg.writeEnd();

if (events.size() > 1) {
    jg.writeEnd();
}

But this ends up into a field which simply contains the String value in quotes.

2

There are 2 best solutions below

3
Sach_IN On

You can create a JSON Object as below statically

{
    "newValue": "someValue",
    "storedFilters": []
}

Then you can add the javax.json.stream.JsonGenerator generated value to the JSON Array storedFilters dynamically.

3
Rahul Agrawal On

You could try below implementation, if it works for you.

String str = "[\n" + "    {\n" + "        \"ref\": \"idValue\",\n" + "        \"data\": {\n"
                + "            \"filter\": [\n" + "                {\n"
                + "                    \"property\": \"number\"\n" + "                }\n" + "            ],\n"
                + "            \"stateId\": \"contacts\"\n" + "        }\n" + "    }\n" + "]";
        JsonFactory factory = new JsonFactory();
        StringWriter jsonObjectWriter = new StringWriter();
        JsonGenerator generator = factory.createJsonGenerator(jsonObjectWriter);

        generator.useDefaultPrettyPrinter(); // pretty print JSON

        generator.writeStartObject();
        generator.writeFieldName("newValue");
        generator.writeString("someValue");
        generator.writeFieldName("storedFilters");
        generator.writeRawValue(str);
        generator.writeEndObject();
        generator.close(); // to close the generator
        System.out.println(jsonObjectWriter.toString());

Output:

{
  "newValue": "someValue",
  "storedFilters": [
    {
      "ref": "idValue",
      "data": {
        "filter": [
          {
            "property": "number"
          }
        ],
        "stateId": "contacts"
      }
    }
  ]
}