0

I'm having troubles with JSon unmarshalling with JAXB MOXy.

Below is the JSON I want to parse:

{
    "accounts": [{
        "description": "A",
        "balance": 1000,
        "balanceAvailable": 1000
    },
    {
        "description": "B",
        "balance": 1001,
        "balanceAvailable": 1001
    },
    {
        "description": "C",
        "balance": 1002,
        "balanceAvailable": 1002
    }],
    "cardPermissions": null,
    "totalBalanceAvailable": 1046.19
}

The problem here is with the field "cardPermissions".

I'm trying to unmarshal an object which doesn't have "cardPermissions" field (no @XmlElement annotation and class attribute).

JAXB throws a NullPointerException while unmarshalling this string because it is:

"cardPermissions": null

and not:

"cardPermissions": "null"

To accept the null value, I had to add inside my POJO the field cardPermissions (with @XmlElement annotation).

Doing this, also without getters and setters, JAXB can unmarshall the provided JSON correctly.

null and "null" are completely different things but I do not want to include this field inside my POJO, and I have to ignore these null values.

EDIT

If i include the root element ("customerInfo") like this:

{
    "customerInfo": {
        "accounts": [{
            "description": "B",
            "balance": 1000,
            "balanceAvailable": 1000
        },
        {
            "description": "C",
            "balance": 1001,
            "balanceAvailable": 1001
        },
        {
            "description": "D",
            "balance": 1002,
            "balanceAvailable": 1002
        }],
        "cardPermissions": null,
        "totalBalanceAvailable": 1046.19
    }
}

JSON gets parsed correctly, but i do not want to include root when i unmarshall the json.

This is my POJO:

@XmlRootElement(name = "customerInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class CustomerInfo {

    @XmlElement(name = "name")
    private String firstName;

    @XmlElement(name = "surname")
    private String lastName;

    @XmlElement
    private String customerId;

    @XmlElement
    private String email;

    @XmlElement
    private String cardPermissions; //This field has no getter and setter. I need it in order to parse correctly the JSON without the root node "customerInfo"

    @XmlElement
    private List<Account> accounts;

    public CustomerInfo() {

    }

    public CustomerInfo(String firstName, String lastName, String emailAddress) {

        this.firstName = firstName;
        this.lastName = lastName;
        this.email = emailAddress;
    }

    public String getFullName() {

        return firstName + " " + lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List<Account> getAccounts() {
        return accounts;
    }

    public void setAccounts(List<Account> accounts) {
        this.accounts = accounts;
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }
}
Lazarus
  • 147
  • 13

1 Answers1

0

You have two choices :

  • Set the attribute required to false in the annotation @XmlElement
  • Set a value to the attribute name in the annotation @XmlElement of all the other fields and then delete the unwanted field. By doing this, the field will be ignored.

For more and good details go to this old post.

Harry Coder
  • 2,429
  • 2
  • 28
  • 32
  • The problem here is that JAXB doesn't ignore that specific value, but it does if i include the root element in the json (SEE edit:) – Lazarus Oct 02 '18 at 13:19
  • Post your POJO for more details. – Harry Coder Oct 02 '18 at 14:20
  • Your json file is valid and your pojo too. Instead of the contructor with parameters you should remove. You are doing wright by setting the root element. Why do you want to remove the root element? – Harry Coder Oct 02 '18 at 15:45
  • Because the web service i call returns this kind of response, and i can't modify it. – Lazarus Oct 02 '18 at 16:32
  • There is a misunderstood of what you want. Please can you clearly explain what you are trying to do? I am still not able to understand what kind of modification you want perform in the JSON. When your JSON is in your POJO, you can then do all the modifications you want and marshall it again to JSON. – Harry Coder Oct 02 '18 at 21:01
  • I just want to parse it correctly. But using the JSON without the root element or without adding the cardPermissions field, JAXB throws a nullpointer exception. This gets fixed in the way i said, but i do not want to add root element when i unmarshall and i do not want to add the cardPermissions field inside my POJO. Thank you for your help – Lazarus Oct 03 '18 at 04:10