Updating JSON using Jackson Streaming API without reading everything in memory

74 views
Skip to first unread message

Lalit Patil

unread,
Oct 14, 2019, 5:28:09 PM10/14/19
to jackson-user
Hi

I am trying to use Jackson streaming API to update data in a JSON file. I want to achieve this without reading the entire file or creating a copy of the file. For example, I might want to update the age of index 2 below from 40 to 20. I could read the entire tree, and then update the node and write the entire tree back. However, I am hoping to read each node and when I find index 1, update only that index and write in the same file without having to write the rest of the data.

Is it possible to achieve this using Jackson?


[
  {
    "index": 0,
    "age": 36,
    "name": {
      "first": "Hernandez",
      "last": "Rutledge"
    }
  },
  {
    "index": 1,
    "age": 40,
    "name": {
      "first": "Clarissa",
      "last": "Kent"
    }
  },
  {
    "index": 2,
    "age": 34,
    "name": {
      "first": "Carver",
      "last": "Melton"
    }
  }
]

Lalit Patil

unread,
Oct 14, 2019, 5:47:36 PM10/14/19
to jackson-user
Just to update, I tried the JSON Parser to read the nodes and modify those. But, I am struggling with updating those nodes in the file again, because with JSONParser, I can read, and JSONGenerator just overwrites the entire file.

Tatu Saloranta

unread,
Oct 15, 2019, 1:13:13 PM10/15/19
to jackson-user
On Mon, Oct 14, 2019 at 2:47 PM Lalit Patil <lpa...@gmail.com> wrote:
>
> Just to update, I tried the JSON Parser to read the nodes and modify those. But, I am struggling with updating those nodes in the file again, because with JSONParser, I can read, and JSONGenerator just overwrites the entire file.

I don't think you can modify JSON documents in place: you will usually
write a new file, delete old (and possible replace new as old).
You may be able to do reading and writing incrementally without
reading the whole contents, but whether this is possible depends on
kind of operation you need to do; whether it needs to cross-reference
information or not.

-+ Tatu +-


>
> On Monday, October 14, 2019 at 5:28:09 PM UTC-4, Lalit Patil wrote:
>>
>> Hi
>>
>> I am trying to use Jackson streaming API to update data in a JSON file. I want to achieve this without reading the entire file or creating a copy of the file. For example, I might want to update the age of index 2 below from 40 to 20. I could read the entire tree, and then update the node and write the entire tree back. However, I am hoping to read each node and when I find index 1, update only that index and write in the same file without having to write the rest of the data.
>>
>> Is it possible to achieve this using Jackson?
>>
>>
>> [
>> {
>> "index": 0,
>> "age": 36,
>> "name": {
>> "first": "Hernandez",
>> "last": "Rutledge"
>> }
>> },
>> {
>> "index": 1,
>> "age": 40,
>> "name": {
>> "first": "Clarissa",
>> "last": "Kent"
>> }
>> },
>> {
>> "index": 2,
>> "age": 34,
>> "name": {
>> "first": "Carver",
>> "last": "Melton"
>> }
>> }
>> ]
>
> --
> 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/de10cf7a-e2bf-453d-83c9-493239186e17%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages