Search code examples
javajsonjacksonpojo

JSON key with blank spaces to a Java POJO


I have following JSON and the problem is with Win the match tag because of the blank spaces:

{
    "api": {
        "results": 10,
        "odds": {
            "Win the match": {
                "1": {
                    "label": "1",
                    "pos": "1",
                    "odd": "1.51"
                },
                "2": {
                    "label": "2",
                    "pos": "3",
                    "odd": "4.90"
                },
                "N": {
                    "label": "N",
                    "pos": "2",
                    "odd": "3.05"
                }
            }
        }
    }
}

And in one of my Java Class POJO:

import com.fasterxml.jackson.annotation.JsonProperty;

public class OddsClass {

    private The1_N22_EMT winTheMatch;

    @JsonProperty("Win the match")
    public The1_N22_EMT getWinTheMatch() { return winTheMatch; }
    @JsonProperty("Win the match")
    public void setWinTheMatch(The1_N22_EMT value) { this.winTheMatch = value; }

}

The problem is that this property is not map properly. Please, I need your help to make the correct mapping.


Solution

  • EDIT

    JSON payload is simpler than you think. For dynamic keys use Map. In your case you have Map in Map because you have two levels of dynamic keys. Sometimes generated POJO from JSON is very complicated. You should generate POJO model from JSON Schema. Your model could look like below:

    class ApiWrapper {
    
        private Api api;
    
        // getters, setters, toString
    }
    
    class Api {
    
        private int results;
        private Map<String, Map<String, Match>> odds;
    
        // getters, setters, toString
    }
    
    class Match {
    
        private String label;
        private String pos;
        private String odd;
    
        // getters, setters, toString
    }
    

    Before edit

    You have two tricky cases:

    • Key with spaces - need to use JsonProperty annotation.
    • Object with dynamic keys - it should be mapped to Map<String, ?>.

    Simple example with POJO model:

    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import java.io.File;
    import java.util.Map;
    
    public class JsonApp {
    
        public static void main(String[] args) throws Exception {
            File jsonFile = new File("./resource/test.json").getAbsoluteFile();
    
            ObjectMapper mapper = new ObjectMapper();
    
            ApiWrapper api = mapper.readValue(jsonFile, ApiWrapper.class);
    
            System.out.println(api);
        }
    }
    
    class ApiWrapper {
    
        private Api api;
    
        // getters, setters, toString
    }
    
    class Api {
    
        private int results;
        private Odds odds;
    
        // getters, setters, toString
    }
    
    class Odds {
    
        @JsonProperty("Win the match")
        private Map<String, Match> winTheMatch;
    
        // getters, setters, toString
    }
    
    class Match {
    
        private String label;
        private String pos;
        private String odd;
    
        // getters, setters, toString
    }
    

    See also: