How to accept a single value as an array in JSON using Java?

Problem Statement

Sometimes, we might get two types of values for a single key – a single value or an array of those values, but we don’t know which will come and when.

Examples of such JSON

{
  "index": 1,
  "value": "hello"
}
{
  "index": 1,
  "value": [
    "hello",
    "codekru"
  ]
}

So, the question that comes into mind is how can we deserialize these kinds of JSON where we might never know what type of value it can have ( a single value or an array of those values ).

Solution

We can solve this problem by using the @JsonFormat annotation. Let’s make one class that will be serialized or deserialized along with the JsonFormat annotation, which will be placed on the top of the field accepting different kinds of values ( In our case, it is the value field ). Please look at this article if you want to know more about the JsonFormat annotation.

Here we will be using the “with” attribute of the @JsonFormat annotation as illustrated below. We will use the ACCEPT_SINGLE_VALUE_AS_ARRAY value of the Feature enum, which signifies that a single value will be treated as an array and then mapped to the corresponding field or property.

Demo.class

import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Feature;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Demo {

	@JsonProperty("index")
	private Integer index;
	
	@JsonProperty("value")
	@JsonFormat( with = Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
	private List<String> value = null;

	// getters and setters
	public Integer getIndex() {
		return index;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

	public List<String> getValue() {
		return value;
	}

	public void setValue(List<String> value) {
		this.value = value;
	}

}
Deserialization

Here, we will have to enable ACCEPT_SINGLE_VALUE_AS_ARRAY while deserializing the array, as illustrated below.

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Codekru {

	public static void main(String[] args) throws JsonProcessingException {
		
		String jsonWithSingleValue = "{\"index\":1,\"value\":\"hello\"}";
		String jsonWithArray = "{\"index\":1,\"value\":[\"hello\",\"codekru\"]}";
		
		ObjectMapper mapper = new ObjectMapper();
		mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
		
		Employee person1 = mapper.readValue(jsonWithSingleValue, Employee.class);
		Employee person2 = mapper.readValue(jsonWithArray, Employee.class);
		
		System.out.println(person1.getValue().toString());
		System.out.println(person2.getValue().toString());

	}
}

Output –

[hello]
[hello, codekru]

Here, a single value is treated as an array and mapped to the corresponding field.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Related Articles –
Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *