Is there a way to move a particular JSON key to the top of the JSON which has been created using the Jackson JsonGenerator?

14 views
Skip to first unread message

Aravinda Baliga B

unread,
Jul 20, 2021, 4:33:52 AM7/20/21
to jackson-user
I am creating a JSON using the `Jackson JsonGenerator` approach and everything seems to work perfectly fine apart from a minor glitch. I would like to move the particular key present in the created JSON to the top of the JSON.

I am creating a key `schema` which appears at the bottom of the JSON. I would like to know if there is a way to move it to the top of the JSON after finished creating the JSON.


Following is the JSON that I am creating:
```
public class JacksonTest {
    public static void main(String[] args) throws IOException {
        StringWriter jsonObjectWriter = new StringWriter();
        JsonGenerator jsonGenerator = new JsonFactory().createGenerator(jsonObjectWriter).useDefaultPrettyPrinter();
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("a", "Value-A");
        jsonGenerator.writeStringField("c", "Value-C");
        jsonGenerator.writeStringField("b", "Value-B");
        jsonGenerator.writeStringField("schema", "2.0");
        jsonGenerator.close();
        jsonGenerator.flush();
        System.out.println(jsonObjectWriter.toString());
    }
}

```

Following is the output I am getting:
```
{
  "a" : "Value-A",
  "c" : "Value-C",
  "b" : "Value-B",
  "schema" : "2.0"
}
```

I would like to convert the JSON something like this:
```
{
  "schema" : "2.0",
  "a" : "Value-A",
  "c" : "Value-C",
  "b" : "Value-B"
}
``` 

I would like to fetch the `schema` from my created JSON and add it to the top of the JSON. 

`Please Note:`
1. I am aware the JSON order does not matter but I am doing this for better readability purposes. If there is a way then it would be really useful for the reader to understand the JSON better.

2. I am aware that I can create the `schema` at first in this code but this is just a sample application that I have provided for ease of understanding. In my real application, I have a `Map` that is populated dynamically throughout the execution and I am adding it at the end of the JSON creation. If I add at the first then I will miss out on a few of the values which are populated after creation so I am adding at the end but I want this to appear at the top of the JSON so trying to move to the top.

3. I want to know if there is a direct way to do it rather than looping over the JSON, as my created JSON in the real application can be pretty large.

I would really appreciate it if there was a way to do it or is there any workaround for this.

Mantas Gridinas

unread,
Jul 20, 2021, 8:37:07 AM7/20/21
to jackso...@googlegroups.com
I suspect using linked hash map should solve your usecase. Ex.

Map mergeAndAddSchema(Map map) {
LinkedHashMap lhm = new LinkedHashMap();
lhm.put("schema", "2.0");
lhm.putAll(map);
return lhm
> --
> You received this message because you are subscribed to the Google Groups "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/e248b04c-5a89-4f0d-af7d-117686eafa9bn%40googlegroups.com.



--
// Mantas

Tatu Saloranta

unread,
Jul 20, 2021, 1:58:09 PM7/20/21
to jackson-user
Streaming generator outputs things exactly in order you specify: only encoding buffer is kept in memory. So there is no way to inject/reorder/change output at that level.

But perhaps you should consider using databind level, constructing `ObjectNode` (mapper.createObjectNode()), modifying that, then serializing as JSON?

-+ Tatu +-


 
Reply all
Reply to author
Forward
0 new messages