Over a million developers have joined DZone.

Binding to JSON & XML - Handling Null

DZone's Guide to

Binding to JSON & XML - Handling Null

· Java Zone ·
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

In a previous post I demonstrated how EclipseLink MOXy can be leveraged to produce both XML and JSON representations of your domain model.  The same metadata is used for both representations and MOXy applies it to leverage the capabilities of the media type.  In this post  I'll focus on how null is handled in each of these representations.

Domain Model

By default a JAXB (JSR-222) implementation will not include a mapped field/property with a null value in the output.  If you want the null value represented then you simply add the following annotation @XmlElement(nillable=true).

package blog.json.nillable;

import javax.xml.bind.annotation.*;

public class Customer {

    private String firstName;
    private String middleName;
    private String lastName;

    public String getFirstName() {
        return firstName;

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

    public String getMiddleName() {
        return middleName;

    public void setMiddleName(String middleName) {
        this.middleName = middleName;

    public String getLastName() {
        return lastName;

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



In the demo code below we will set both the middleName and lastName properties to null. Since we have mapped these properties differently in the domain model we will examine the output to see the impact of using @XmlElement(nillable=true).

package blog.json.nillable;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        Customer customer = new Customer();
        JAXBContext jc = JAXBContext.newInstance(Customer.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        // Output XML
        marshaller.marshal(customer, System.out);

        // Output JSON
        marshaller.setProperty("eclipselink.media-type", "application/json");
        marshaller.marshal(customer, System.out);


XML Output

By default JAXB implementations do not include null values in the output, so there is no element corresponding to the middleName property.  Since we annotated the lastName property with @XmlElement(nillable=true) and it had a null value, it is represented in the output.  In XML an element with a null value is represented by adding the xsi:nil="true" attribute.

<?xml version="1.0" encoding="UTF-8"?>
   <lastName xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

JSON Output

Just like in the XML output, an entry for the middleName property does not appear in the JSON output. Since we annotated the lastName property with @XmlElement(nillable=true) and it had a null value, it is represented in the output.  In JSON a null value is represented with null

   "customer" : {
      "firstName" : "Jane",
      "lastName" : null

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}