I want to edit only one value in an existing JSON file.
Is there any way to do that without parsing and re-writing the whole file? (I use Jackson Streaming API to generate and parse the file, but I’m not sure that Streaming API can do that).
my Example.json file contains the following:
{
"id" : "20120421141411",
"name" : "Example",
"time_start" : "2012-04-21T14:14:14"
}
Example given: I want to edit the value of the “name” from “Example” to “other name”.
Not that I know of; either at JSON level, or at file level — unless length of the values happened to be exactly same, underlying file system typically requires rest of the file to be rewritten from point of change.
You can read and write file using Streaming API, replacing value on the go; see
JsonGenerator.copyCurrentEvent(jp)to simplify the task — it just copies the input event exactly as is. For everything except for replacing particular value, you can call that; and for value, can callJsonGenerator.writeString().