6

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?

This is how I'm handling this right now:

  • an empty string is represented as " "
  • an empty int/float/double/bool/etc. is represented as NULL

Is this correct?

Austen Stone
  • 948
  • 1
  • 10
  • 21
  • Setting an empty string is like enforcing a default value. So if you do this, you should do it for other types as well, int - asign a 0, bool asign false etc. However, in my opinion "empty data" should equivalent to null. So here you have to think about whether your data should be empty or not present at all, cause you can have undefined checks somewhere as well. – Samuil Petrov Aug 25 '17 at 13:21
  • 1
    Possible duplicate of [Representing null in JSON](https://stackoverflow.com/questions/21120999/representing-null-in-json) – cassiomolin Aug 25 '17 at 14:34

2 Answers2

7

It should be pretty straight forward.

{
  "Value1": null, 
  "Value2": null,
}

Null represents a null-able datatype so your business layer needs then to know if that is an int, double, string ...

Frank
  • 735
  • 1
  • 12
  • 33
Jester
  • 3,069
  • 5
  • 30
  • 44
0

It is entirely up to you.

The falsy values in JavaScript are:

  • false
  • null
  • undefined
  • 0
  • NaN
  • '' or ""

You can use any of these to represent an empty value. However, keep in mind that when you try to access an object's property which doesn't exist, you will get undefined. So undefined can be considered the standard null value, in a way.

Having said that, one convention is to use null so that you can distinguish a property that has been intentionally set to null from a property that is actually undefined.

Simone
  • 20,302
  • 14
  • 79
  • 103