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.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jackson-user/e248b04c-5a89-4f0d-af7d-117686eafa9bn%40googlegroups.com.