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

Related

  • Aggregating REST APIs Calls Using Apache Camel
  • GraphQL vs REST API: Which Is Better for Your Project in 2025?
  • Implementing Budget Policies and Budget Limits on Databricks
  • How To Call Cohere and Hugging Face AI From Within an Oracle Database Using JavaScript

Trending

  • Mocking Kafka for Local Spring Development
  • Why Your Test Automation Is Always Behind the Code And the Architecture That Fixes It
  • Your AI Agent Tests Are Passing, But Your Agent Is Still Broken
  • Feature Flag Debt: Performance Impact in Enterprise Applications
  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.9K 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
  • GraphQL vs REST API: Which Is Better for Your Project in 2025?
  • Implementing Budget Policies and Budget Limits on Databricks
  • How To Call Cohere and Hugging Face AI From Within an Oracle Database Using JavaScript

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook