Search code examples
javajacksonjackson-databindjackson-dataformat-xml

Adding property inside property in jackson


I have a specific Geojson structure (feature) stored as string see example below:

{
  "type": "Feature",
  "properties": {
    "scalerank": 2,
    "adm1_code": "USA-3514",
    "diss_me": 3514,
    "adm1_cod_1": "USA-3514",
    "iso_3166_2": "US-MN"
  },
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        ....
      ]
    ]
  }
}

I want to append and make some modifications on that geojson. So what I did is first map this structure to a Jackson ObjectNode using:

           ObjectNode bufferFeature = mapper.valueToTree(bufferPolygon);

Then I want to append some elements inside properties to add some new properties. I know how to add a property normally without specifying the a parent, I do that using bufferFeature.put("prop1","value");

So my questions are: How to add new property inside a specific property ?


Solution

  • You must use the ObjectNode corresponding to the object to which you want to add your property. Try, for example: bufferFeature.with("properties").put("prop1", "value").

    See the answer to this question.