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
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Aggregating REST APIs Calls Using Apache Camel
  • How To Call Cohere and Hugging Face AI From Within an Oracle Database Using JavaScript
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?
  • What Is API-First?

Trending

  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  • Optimizing Integration Workflows With Spark Structured Streaming and Cloud Services
  • How To Develop a Truly Performant Mobile Application in 2025: A Case for Android
  • Cosmos DB Disaster Recovery: Multi-Region Write Pitfalls and How to Evade Them
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Representing Null Values in a REST JSON Response

Representing Null Values in a REST JSON Response

Null vs. undefined. See how one author chooses to represent null values when dealing with REST JSON responses.

By 
Ganesh Gembali user avatar
Ganesh Gembali
·
Aug. 14, 16 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
68.5K Views

Join the DZone community and get the full member experience.

Join For Free

REST has become the de facto standard for serving data to external consumers. I have come across an interesting, but quite generic case — what do I do with attributes with null values in a JSON response. This a very basic but a very important question, and there is a lot of discussion around it with a lot of diverse opinions. Here is an aggregated analysis of how you can take care of this scenario in your own context.

For any data entity, some attributes are required and some are optional. So the question is, "What if there is no value for an optional field?" How do we represent that in our response?

A common way to handle the problem is by coding this response:

: ""(blank) or any character != null != "null" :

This basic data violation. Representing null values as any kind of string in JSON response is not right. At least, it's not intuitive for a consumer. This option is an absolute "NO." We have the following two options:

{
    "requiredField":"Some value must be here",
    "someOptionalField":null
}
//OR
{
    "requiredField":"Some value must be here",
}


Null vs. Undefined

In JavaScript, having an attribute in JSON with null and not having one (undefined) are completely different. Read more here. Consider following clauses:

Attribute with nullAttribute absent
data.someOptionalField == nulltruetrue
data.someOptionalField === nulltruefalse
data.someOptionalField === undefinedfalsetrue


If your consumer has different processing for null and undefined, then you must go with the attribute present with a null value in data (first format). Otherwise, I would suggest the second format because of one simple reason: smaller payload. This would be a big gain, especially when there are many optional fields.

There were also some arguments about readability. There are two solutions to this. One, ask the consumer to refer to the REST API documentation, which can be easily created with plenty of tools these days. Another is something I have seen with mygov.in data APIs (by the Indian government). A response contains required information from an information entity, like all attributes and their type. A second approach with a flag that omits that information would also be nice.

Basically, the decision depends on your context. If you have more suggestions please share them.

JSON REST Web Protocols

Opinions expressed by DZone contributors are their own.

Related

  • Aggregating REST APIs Calls Using Apache Camel
  • How To Call Cohere and Hugging Face AI From Within an Oracle Database Using JavaScript
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?
  • What Is API-First?

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • 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: