0

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.

Timz
  • 412
  • 2
  • 13

2 Answers2

0

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.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Sach_IN
  • 31
  • 7
  • Thanks for your suggestion, but the "newValue" should be only an example. The result is dynamically and can be way bigger. I will update my description to make this clear. – Timz Feb 26 '20 at 08:40
  • ok let it be, you will have a JSON from javax.json.stream.JsonGenerator. So add a key directly. Say result is the JSON means, do add result.put("storedFilters", DBValues) will get your requirement – Sach_IN Feb 26 '20 at 08:57
  • It works, but I work with a ServletOutputStream and the insert into it feels a little dirty to me. Also the format use not like using the JsonGenerator. Nevertheless, thanks for the suggestion. – Timz Feb 26 '20 at 12:03
0

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"
      }
    }
  ]
}
Rahul Agrawal
  • 623
  • 1
  • 5
  • 18
  • In your example you use the JsonGenerator from Jackson, right? Because in this implementation the javax.json.stream.JsonGenerator is used and I cannot simply switch, because the generic implementations use this generator a lot. – Timz Feb 26 '20 at 09:14
  • Oh yes used Jackson. But I think issue in your approach is you are simply writing the array as object. You should use something like writeRaw or writeArray or some similar methods for javax JsonGenerator – Rahul Agrawal Feb 26 '20 at 09:21
  • The problem is that I didn't found such a functionality in javax.json. – Timz Feb 26 '20 at 09:23