1

What is the beast way to deal with the JSON object, that has the following structure:

{
  "Parameters": {
    "0": {
      "some key 0": "some value 0"
    },
    "1": {
      "some key 1": "some value 1"
    },
    "2": {
      "some key 2": "some value 2"
    },
    ....
    "n": {
      "some key n": "some value n"
    }
}

It contains properties from 0 to n (e.g. 100), each property is an object with single key value. Looks like all keys are different.

Is it possible to transform it into a list of Parameter, where each parameter has next structure:

public class Parameter {
    String key;
    String value;
}

What is the best way to handle this in jackson?

7
  • use ObjectMapper @RostyslavRoshak Commented Feb 17, 2017 at 10:01
  • check this: stackoverflow.com/questions/6349421/… Commented Feb 17, 2017 at 10:02
  • I don't know the names of keys for inner JSON objects. Commented Feb 17, 2017 at 10:08
  • you mean Parameters that dynamic ? @RostyslavRoshak Commented Feb 17, 2017 at 10:10
  • What I mean is that for the inner object: {"some key 0": "some value 0"}, the value of the key is unknown. And I would like to handle it as key in my Parameter POJO. Commented Feb 17, 2017 at 10:14

2 Answers 2

2

If keys are dynamic then we can use @JsonAnySetter annotation.

You can try something like this:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

Parameters parameters = mapper.readValue(jsonData, Parameters.class);

Where content of Parameter class would be:

@JsonRootName("Parameters")
class Parameters {
    private List<Map<String, String>> parameters = new ArrayList<Map<String, String>>();

    @JsonAnySetter
    public void setDynamicProperty(String name, Map<String, String> map) {
        parameters.add(map);
    }

    public List<Map<String, String>> getParameters() {
        return parameters;
    }

    public void setParameters(List<Map<String, String>> parameters) {
        this.parameters = parameters;
    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

The problem is that we don't have array of objects in the json. "0", "1", are fields of the json object.
Yes, thats why I have used @JsonAnySetter annotation.
Sorry, just made some small test, everything is working as expected.
1

I think Sachin's approach is the correct direction, but I would amend it in the following way, and remove some of the nested maps that it ends up with:

@JsonRootName("Parameters")
public class Parameters {
    private List<Parameter> parameters = new ArrayList<>();

    @JsonAnySetter
    public void setDynamicProperty(String _ignored, Map<String, String> map) {
        Parameter param = new Parameter();
        param.key = map.keySet().iterator().next();
        param.value = map.values().iterator().next();
        parameters.add(param);
    }

    public List<Parameter> getParameters() {
        return parameters;
    }
}

public class Parameter {
    public String key, value;
}

After deserialization, the getParameters() method will return a list of Parameter instances. Given your example input, its structure will look like this, when serialized to JSON :

[
  {
    "key": "some key 0",
    "value": "some value 0"
  },
  {
    "key": "some key 1",
    "value": "some value 1"
  },
  {
    "key": "some key 2",
    "value": "some value 2"
  },
  {
    "key": "some key n",
    "value": "some value n"
  }
]

Note that the extraction of key and value uses an iterator which will throw an exception if it encounters an empty object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.