DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Validate XML Request Against XML Schema in Mule 4
  • How to Convert JSON to XML or XML to JSON in Java
  • How to Connect to Splunk Through Anypoint Studio in 10 Steps
  • How to Convert Data to JSON and XML in Java

Trending

  • LTS JDK 21 Features
  • Auto-Scaling DynamoDB Streams Applications on Kubernetes
  • LLMs for Bad Content Detection: Pros and Cons
  • Exploring Sorting Algorithms: A Comprehensive Guide
  1. DZone
  2. Coding
  3. Languages
  4. Binding to JSON & XML - Handling Null

Binding to JSON & XML - Handling Null

Blaise Doughan user avatar by
Blaise Doughan
·
Apr. 25, 12 · Interview
Like (0)
Save
Tweet
Share
22.91K Views

Join the DZone community and get the full member experience.

Join For Free
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.*;

@XmlRootElement
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;
    }

    @XmlElement(nillable=true)
    public String getLastName() {
        return lastName;
    }

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

}

Demo

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();
        customer.setFirstName("Jane");
        customer.setMiddleName(null);
        customer.setLastName(null);
        
        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"?>
<customer>
   <firstName>Jane</firstName>
   <lastName xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</customer>

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
   }
}
JSON XML Binding (linguistics)

Published at DZone with permission of Blaise Doughan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Validate XML Request Against XML Schema in Mule 4
  • How to Convert JSON to XML or XML to JSON in Java
  • How to Connect to Splunk Through Anypoint Studio in 10 Steps
  • How to Convert Data to JSON and XML in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: