As an empty string is not valid JSON it would be incorrect for JSON.parse('') to return null because "null" is valid JSON. e.g.

JSON.parse("null");

returns null. It would be a mistake for invalid JSON to also be parsed to null.

While an empty string is not valid JSON two quotes is valid JSON. This is an important distinction.

Which is to say a string that contains two quotes is not the same thing as an empty string.

JSON.parse('""');

will parse correctly, (returning an empty string). But

JSON.parse('');

will not.

Valid minimal JSON strings are

The empty object '{}'

The empty array '[]'

The string that is empty '""'

A number e.g. '123.4'

The boolean value true 'true'

The boolean value false 'false'

The null value 'null'

Answer from bhspencer on Stack Overflow
🌐
Reddit
reddit.com › r/learnprogramming › best practices to send for empty values of a json api?
r/learnprogramming on Reddit: Best Practices to send for Empty Values of a JSON API?
April 22, 2024 -

What do you choose? Getting lots of conflicting answers.

  1. Null

  2. Undefined (Omitted from JSON)

  3. Empty of that type?

    1. String = ""

    2. Number = 0

    3. Boolean = false

    4. Array = []

    5. Object = {}

CMS JSON API to be consumed by JS Framework Frontend

Discussions

web api - Use empty string, null or remove empty property in API request/response - Software Engineering Stack Exchange
To me, setting it as null would ... parent value." ... Since "Remove empty property" is also why to "avoid" a null (it is true that null is avoided as such), the questioner means "Return non-null" [Object] (i.e: empty String, empty Array, etc) when they write "avoid". ... The first thing to bear in mind is that applications at their edges are not object-oriented (nor functional if programming in that paradigm). The JSON that you receive ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
JSON empty string - Stack Overflow
Old question - but its the top result when you search for 'json stringify empty string' so I'll share the answer I found. This appears to be a bug in certain versions of IE8, where empty DOM elements return a value which looks like an empty string, evaluates true when compared to an empty string, ... More on stackoverflow.com
🌐 stackoverflow.com
cjson - What is the JSON Standard for an empty string or null variable? - Stack Overflow
I'm building an application that parses a JSON template and then replaces the values of the objects with new data. My question is what is the standard way to represent empty data in JSON? ... Setting an empty string is like enforcing a default value. So if you do this, you should do it for ... More on stackoverflow.com
🌐 stackoverflow.com
What is the convention in JSON for empty vs. null?
And additionally, will a zero-length string for a non-string object be treated as a null? ... Save this answer. ... Show activity on this post. It is good programming practice to return an empty array [] if the expected return type is an array. This makes sure that the receiver of the json can treat the value ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Hiredgun
hiredgun.tech › home › apis › handling empty json strings
Handling Empty JSON Strings - hiredgun.tech
December 8, 2024 - If the first paramater doesn’t exist (such as an empty string), then the second parameter is used instead. So in our particular case, if a value exists in a particular key/value pair JSON object it is used, but an empty string is replace by null.
🌐
Medium
medium.com › @H2bm › choosing-between-null-empty-string-or-unknown-for-json-variables-in-java-d3e090d8ae0c
Choosing Between Null, Empty String, or “Unknown” for JSON Variables in Java. Are Your Missing Values Intentional? | by H2bm | Medium
February 15, 2025 - Clear Intent: null unambiguously denotes that a value does not exist. Space Efficiency: Omitting null fields using annotations like @JsonInclude(JsonInclude.Include.NON_NULL) reduces payload size. Database Compatibility: Many databases differentiate between NULL and empty strings, making null a natural fit for unset fields.
Top answer
1 of 8
39

TLDR; Remove null properties

The first thing to bear in mind is that applications at their edges are not object-oriented (nor functional if programming in that paradigm). The JSON that you receive is not an object and should not be treated as such. It's just structured data which may (or may not) convert into an object. In general, no incoming JSON should be trusted as a business object until it is validated as such. Just the fact that it deserialized does not make it valid. Since JSON also has limited primitives compared to back-end languages, it is often worth it to make a JSON-aligned DTO for the incoming data. Then use the DTO to construct a business object (or error trying) for running the API operation.

When you look at JSON as just a transmission format, it makes more sense to omit properties that are not set. It's less to send across the wire. If your back-end language does not use nulls by default, you could probably configure your deserializer to give an error. For example, my common setup for Newtonsoft.Json translates null/missing properties to/from F# option types only and will otherwise error. This gives a natural representation of which fields are optional (those with option type).

As always, generalizations only get you so far. There are probably cases where a default or null property fits better. But the key is not to look at data structures at the edge of your system as business objects. Business objects should carry business guarantees (e.g. name at least 3 characters) when successfully created. But data structures pulled off the wire have no real guarantees.

2 of 8
23

Going with an empty string is a definitive no. Empty string still is a value, it is just empty. No value should be indicated using a construct which represents nothing, null.

From API developer's point of view, there exist only two types of properties:

  • required (these MUST have a value of their specific type and MUST NOT ever be empty),
  • optional (these MAY contain a value of their specific type but MAY also contain null.

This makes it quite clear that when a property is mandatory, ie. required, it can never be null.

On the other hand, should an optional property of an object not be set and left empty, I prefer to keep them in the response anyway with the null value. From my experience it makes it easier for the API clients to implement parsing, as they're not required to check whether a property actually exists or not, because it's always there, and they can simply convert the response to their custom DTO, treating null values as optional.

Dynamically including/removing fields from the response forces including additional conditions on the clients.


Either way, whichever way you choose, make sure you keep it consistent and well documented. That way it really does not matter what you use for your API, as long as the behaviour is predictable.

🌐
Thinking Matters
social-biz.org › 2023 › 12 › 21 › null-values-in-json
NULL Values in JSON | Thinking Matters
December 21, 2023 - If the data is stored into a DB table, then there will be a timestamp when the field was updated. If the key-value is omitted, then no DB update, if it is NULL then I don’t think the data entry is made in the DB and it won’t overwrite any existing value, whilst an empty string will update (or create) a value in the table.
Find elsewhere
🌐
Wordpress
paulzipblog.wordpress.com › 2020 › 08 › 16 › oracle-json-and-empty-strings
Oracle JSON and Empty Strings “” | Paulzip's Oracle Blog
August 16, 2020 - What’s the difference in JSON between null and empty string? Empty String = I have a known string value and it is empty with zero length.
🌐
Make Community
community.make.com › questions
Send empty object if json string is null - Questions - Make Community
July 8, 2024 - I’m trying to detect if a JSON string exists (i.e. not null) and send an empty json object {} instead. First, I use Transform to JSON to turn an object into JSON Then, I use an if statement to check if the object…
🌐
Drupal
drupal.org › project › drupal › issues › 3250298
Return empty string "" with JSON Serializer instead of FALSE [#3250298] | Drupal.org
August 9, 2024 - Problem/Motivation Using JSON Serializer, I'm forced to change some field values to RAW output, in order to display the special characters as they are. Side effect: When the RAW output is enabled, then the EMPTY string is displayed as false "field_address4": false, it should be "" "field_address4": "", Proposed resolution I tried with alter the value using hook_views_pre_render() $field_address4 = !empty($node->get('field_address4')->value) ?
🌐
Stack Overflow
stackoverflow.com › questions › 33424252 › replace-json-null-value-with-empty-string
rest - Replace JSON null value with empty String? - Stack Overflow
You are right, having null isn't bad. In fact, it's correct. Having "" in place of null when the value is actually null would be bad.
🌐
Conrad Akunga
conradakunga.com › blog › handling-null-and-empty-strings-with-system-text-json
Handling Null And Empty Strings With System.Text.Json | Conrad Akunga - Building Software In .NET
March 9, 2021 - public class NullToEmptyStringConverter : JsonConverter<string> { // Override default null handling public override bool HandleNull => true; // Check the type public override bool CanConvert(Type typeToConvert) { return typeToConvert == typeof(string); } // Ignore for this example public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { throw new NotImplementedException(); } // public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) { if (value == null) writer.WriteStringValue(""); else writer.WriteStringValue(value); } }
🌐
Postman
community.postman.com › help hub
Skip Property in JSON File If It Contains an Empty String - Help Hub - Postman Community
January 14, 2022 - I have a JSON File that looks something like this. I want to be able to skip the discounts property if it is empty. [ { "state": "CT", "postalCode": "06010", "species": "Cat", "unit": "years", "value": 0, "gender": "Male", "breed": "Feral", "discounts": "", "reimbursement": 70, "deductible": 250, "annualLimit": 5000 }, { "state": "CT", "postalCode": "06010", "species": "Cat", "unit": "years", "value": 0, "gender": "Male", "breed": "Feral...
🌐
IBM
ibm.com › docs › en › baw › 19.0.0
Handling JSON null and empty arrays and objects
August 11, 2022 - Handling null and empty arrays and objects used in JSON data is described.
🌐
Conrad Akunga
conradakunga.com › blog › handling-null-and-empty-strings-with-systemtextjson-part-2
Handling Null And Empty Strings With System.Text.Json - Part 2 | Conrad Akunga - Building Software In .NET
November 21, 2022 - public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { // Read the string var res = reader.GetString(); // If it is am empty string, return a null if (string.IsNullOrEmpty(res)) return null; else // otherwise, return the read value return res; }
🌐
Boomi
community.boomi.com › s › article › Working-with-Nulls-in-JSON
Article: Working with Nulls in JSON - Boomi Community
March 10, 2025 - Boomi will read a null value and an empty string as the same. Both are viewed as an empty string within a map function. Both will be viewed as if nothing was mapped and the element will not be present in the output JSON. The Data Process step with Find/Replace can not be used within this situation because it would not be able to distinguish the difference between a null element, a null object, and a null array.