• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by User Bot


25 Mar, 2025

Updated at 20 May, 2025

Deserializing heavily nested JSON to Java Object

Below is the JSON which I need to convert to Java Object.

{
    "example": {
        "rule_string": "Elected.Benefit Area~ ++~ Is Eq ual To~ ++~ \"MEDICAL\"~ And~ Ends with the Text~ (~ Elected.Benefit Plan Option~ ,~ \"PPO\"~ )~",
        "rule_source": "Elected.Benefit Area~ Is Eq ual To~ \"MEDICAL\"~ ~And~ Ends with the Text~ (~ Elected.Benefit Plan Option~ ,~ \"PPO\"~~ )~ ~ ",
        "rule_type": "if",
        "ast": {
            "data": "if_statement",
            "children": [
                {
                    "data": "and_test",
                    "children": [
                        {
                            "data": "concat",
                            "children": [
                                {
                                    "data": "field",
                                    "children": [
                                        "Elected.Benefit Area"
                                    ],
                                    "_meta": null
                                },
                                {
                                    "data": "concat",
                                    "children": [
                                        {
                                            "data": "field",
                                            "children": [
                                                "Is Eq ual To"
                                            ],
                                            "_meta": null
                                        },
                                        {
                                            "data": "string",
                                            "children": [
                                                "\"MEDICAL\""
                                            ],
                                            "_meta": null
                                        }
                                    ],
                                    "_meta": null
                                }
                            ],
                            "_meta": null
                        },
                        {
                            "data": "function",
                            "children": [
                                {
                                    "data": "function_name",
                                    "children": [
                                        "Ends with the Text"
                                    ],
                                    "_meta": null
                                },
                                {
                                    "data": "function_args",
                                    "children": [
                                        {
                                            "data": "field",
                                            "children": [
                                                "Elected.Benefit Plan Option"
                                            ],
                                            "_meta": null
                                        },
                                        {
                                            "data": "string",
                                            "children": [
                                                "\"PPO\""
                                            ],
                                            "_meta": null
                                        }
                                    ],
                                    "_meta": null
                                }
                            ],
                            "_meta": null
                        }
                    ],
                    "_meta": null
                }
            ],
            "_meta": null
        },
        "valid": true,
        "parse_error": null
    }
}

I used https://www.jsonschema2pojo.org/ to generate the classes which are as below. I have removed setters & getters in the snippets below. Each of the setters & getters have the @JsonProperty annotation.

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "rule_string",
    "rule_source",
    "rule_type",
    "ast",
    "valid",
    "parse_error"
})
public class Example {

    @JsonProperty("rule_string")
    private String ruleString;
    @JsonProperty("rule_source")
    private String ruleSource;
    @JsonProperty("rule_type")
    private String ruleType;
    @JsonProperty("ast")
    private Ast ast;
    @JsonProperty("valid")
    private Boolean valid;
    @JsonProperty("parse_error")
    private Object parseError;
    @JsonIgnore
    private Map additionalProperties = new LinkedHashMap();
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "data",
    "children",
    "_meta"
})
public class Ast {

    @JsonProperty("data")
    private String data;
    @JsonProperty("children")
    private List children;
    @JsonProperty("_meta")
    private Object meta;
    @JsonIgnore
    private Map additionalProperties = new LinkedHashMap();
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "data",
    "children",
    "_meta"
})
public class Child {

    @JsonProperty("data")
    private String data;
    @JsonProperty("children")
    private List children;
    @JsonProperty("_meta")
    private Object meta;
    @JsonIgnore
    private Map additionalProperties = new LinkedHashMap();
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "data",
    "children",
    "_meta"
})
public class Child__1 {

    @JsonProperty("data")
    private String data;
    @JsonProperty("children")
    private List children;
    @JsonProperty("_meta")
    private Object meta;
    @JsonIgnore
    private Map additionalProperties = new LinkedHashMap();
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "data",
    "children",
    "_meta"
})
public class Child__2 {

    @JsonProperty("data")
    private String data;
    @JsonProperty("children")
    private List children;
    @JsonProperty("_meta")
    private Object meta;
    @JsonIgnore
    private Map additionalProperties = new LinkedHashMap();
}

I suspect the issue lies in the inner most "chidren" node which is an array of Strings where as the other "children" nodes are arrays of objects. Will it be possible to parse this JSON into POJOs using Jackson?