0

I try to read Json file with Jackson and depends on it, make another Json output. The problem is to read Json file, I created a POJO object with fields, that represent in Json. But it could change and there are could be appear another values, that I didn't represent in POJO. So my logic there, is will add fields, depends on read values I've been get from Json. Is there are any way to do this, or I came not right way..

public String makeOutput() {

        Collection<String> key = map.get("POJO");

        for (String name : key) {

            requestOut = mock.reflect(CAPM.class)
                    .field("name", "Anna")
                    .field("age", 12)

// there is field 'name', that didn't represent in POJO

                    .field(name, "Green")
                    .map(gson::toJson)
                    .val();

        }

        return requestOut;
    }

I'm using mockNeat lib. for generate Json. "map" is a MultiValuedMap<String, String>

Spike Johnson
  • 365
  • 4
  • 12
  • To know which fields are in the JSON file first load the file content into a Map ([Convert json to map](https://stackoverflow.com/questions/443499/convert-json-to-map) details how this can be done) and then loop over the map and build the requestOut object as in your example –  Nov 29 '18 at 13:50
  • @ValentinCarnu It doesn't help. It's no problem to read `Json` and look what fields are there. If I take from it field, that doesn't represent in `JavaBean` class - it shouldn't work. – Spike Johnson Nov 30 '18 at 09:15
  • To dynamically add fields to a class you will need to use byte code instrumentation libraries, [here](https://stackoverflow.com/a/18275466/10386912) are some details –  Nov 30 '18 at 11:15

0 Answers0