Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@JsonProperty name with spaces

Tags:

java

json

jackson

Is it allowed to have spaces in @JsonProperty name? i.e.

@JsonProperty("Jon Snow")
private String jonSnow = "Lord";

// getter & setter are omitted

I expect to get the following output:

{
   "Jon Snow" : "Lord"
}

But the actual result is:

{
   "jonSnow" : "Lord"
}

I use jackson-core v2.8.8, jackson-databind v2.8.8.1, and jackson-annotations v2.8.8.

like image 521
naXa Avatar asked Nov 24 '25 20:11

naXa


1 Answers

The problem was that I was using @JsonProperty annotation from the wrong Jackson library.

I had import org.codehaus.jackson.annotate.JsonProperty; (Jackson v1) which I had to change to import com.fasterxml.jackson.annotation.JsonProperty; (Jackson v2), to be consistent with the library I was using.

I've found the answer in comments to this question: @JsonProperty not working as expected

like image 95
naXa Avatar answered Nov 27 '25 08:11

naXa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!