Search code examples
javajsonserializationkibanagetter-setter

JSON returning caret (^) in the value and returning null in the response


I am working on mapping json values with kibana, one element i.e amount it is returning beginning with "^" and its not mapping the getters and setters. How could I map it?, I used @jsonProperty and @jsonCreator as well but still seeing null in the value.

example:

{
  "value": "530b8371",
  "Code": "AH",
  "^amount": "$275,817.49",
  "description": "grant"
}

Solution

  • This is not (necessarily) an answer, but is needed to show code.

    Unable to reproduce the issue posed in the question.

    The following Jackson mapping code using @JsonProperty works fine.

    Imports used below

    import com.fasterxml.jackson.annotation.JsonCreator;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.databind.ObjectMapper;
    

    Option 1: Using @JsonProperty on fields

    class Foo {
        @JsonProperty
        private String value;
        @JsonProperty("Code")
        private String code;
        @JsonProperty("^amount")
        private String amount;
        @JsonProperty
        private String description;
    
        @Override
        public String toString() {
            return "Foo[value=" + this.value + ", code=" + this.code +
                     ", amount=" + this.amount + ", description=" + this.description + "]";
        }
    }
    

    Option 2: Using @JsonProperty on methods

    class Foo {
        private String value;
        private String code;
        private String amount;
        private String description;
    
        @JsonProperty
        public String getValue() {
            return this.value;
        }
        public void setValue(String value) {
            this.value = value;
        }
    
        @JsonProperty("Code")
        public String getCode() {
            return this.code;
        }
        public void setCode(String code) {
            this.code = code;
        }
    
        @JsonProperty("^amount")
        public String getAmount() {
            return this.amount;
        }
        public void setAmount(String amount) {
            this.amount = amount;
        }
    
        @JsonProperty
        public String getDescription() {
            return this.description;
        }
        public void setDescription(String description) {
            this.description = description;
        }
    
        @Override
        public String toString() {
            return "Foo[value=" + this.value + ", code=" + this.code +
                     ", amount=" + this.amount + ", description=" + this.description + "]";
        }
    }
    

    Option 3: Using @JsonCreator on constructor

    class Foo {
        private String value;
        private String code;
        private String amount;
        private String description;
    
        @JsonCreator
        public Foo(@JsonProperty("value") String value,
                   @JsonProperty("Code") String code,
                   @JsonProperty("^amount") String amount,
                   @JsonProperty("description") String description) {
            this.value = value;
            this.code = code;
            this.amount = amount;
            this.description = description;
        }
    
        @Override
        public String toString() {
            return "Foo[value=" + this.value + ", code=" + this.code +
                     ", amount=" + this.amount + ", description=" + this.description + "]";
        }
    }
    

    Test

    String json = "{\r\n" + 
                  "  \"value\": \"530b8371\",\r\n" + 
                  "  \"Code\": \"AH\",\r\n" + 
                  "  \"^amount\": \"$275,817.49\",\r\n" + 
                  "  \"description\": \"grant\"\r\n" + 
                  "}";
    ObjectMapper objectMapper = new ObjectMapper();
    Foo foo = objectMapper.readValue(json, Foo.class);
    System.out.println(foo);
    

    Output (same for all Foo classes above)

    Foo[value=530b8371, code=AH, amount=$275,817.49, description=grant]