7

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?

2
  • 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. Commented Aug 25, 2017 at 13:21
  • 1
    Possible duplicate of Representing null in JSON Commented Aug 25, 2017 at 14:34

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

Comments

-2

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.

4 Comments

undefined is not valid JSON, and NaN isn't valid JSON either.
You're right, however if you pass any of these values to JSON.stringify() you'll always get back a string, which is valid JSON
but JavaScript was never mentioned.
This answer is not even related to the question. E.g. what If I want to know what json values are considered falsy for jq // operator?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.