While using Vert.x, for manipulating JSONs I'm using a JSON Pointer. Recently came across having to do the same with JSON arrays.
Here's the sample code I tried.
String jsonInput = "{\"string\":\"string\",\"json\":{\"items\":[\"item-1\",\"item-2\",\"item-3\"]}}";
JsonObject json = new JsonObject(jsonInput);
JsonPointer pointer = JsonPointer.from("/json/items/0");
System.out.println(pointer.writeJson(json,"new item"));
Consider this as the input
{
"string": "string",
"json": {
"items": [
"item-1",
"item-2",
"item-3"
]
}
}
The pointer to item-1
would be /json/items/0
. When I use the same with JsonPointer from Vert.x instead of replacing the existing item, it ends up adding another element instead of writing at index zero like below
{
"string": "string",
"json": {
"items": [
"<newly-written-item>"
"item-1",
"item-2",
"item-3"
]
}
}
Is it possible to overwrite the existing value instead of adding at the index ?