1

I inherited some legacy Java code which passes me a parameter of type Object.

Inside that I find some List<Map<String, Object>> or a Map<String, List<Object>> (Object here could be the same structures)

I know the drawbacks of that, no need for explantation here. The above is stuff I can not change, no way here to generate POJO's in this case.

The data originates from some JSON like this example:

{
  "accounting" : [
    { "firstName" : "John",
      "lastName"  : "Doe",
      "age"       : 23 },

    { "firstName" : "Mary",
      "lastName"  : "Smith",
      "age"      : 32,
      "address" : {
        "Street": "Dorfstrasse 23",
        "city": "something"        
      }
    }
  ],
  "sales"      : [
    { "firstName" : "Sally",
      "lastName"  : "Green",
      "age"      : 27 },

    { "firstName" : "Jim",
      "lastName"  : "Galley",
      "age"       : 41 }
  ]
} 

Is there a convenient way to manipulate the java object structure like adding

address2 : {
"uvw": "123"
"xyz": "0815"
}

To the path accounting[1].address without manual implementing this structure with JsonNode, iteration the JSON array etc. At the end I want that changes in the Java Object, not as JSON string

Matt
  • 12,848
  • 2
  • 31
  • 53
asterix
  • 33
  • 1
  • 7
  • https://stackoverflow.com/questions/14443321/how-to-dynamically-add-class-fields-at-runtime-in-java may help. also you can use xslt like approach and create the object more elastic https://stackoverflow.com/a/49011455/175554 – ozkanpakdil Jun 08 '21 at 17:01

0 Answers0