Does JSON Have an "undefined" Value?

In JSON, there isn't a direct representation for "undefined" as a value. The JSON specification, as defined in RFC 8259, does not explicitly mention the concept of undefined values. Instead, it defines the following data types:

  1. object;
  2. array;
  3. string;
  4. number;
  5. true;
  6. false;
  7. null.

Any value that doesn't fall into these categories would not be considered valid JSON. If you must represent undefined in JSON, then you can use null, which is ideal for cases where a value is explicitly absent or has no meaningful content. For example:

{
  "name": "John",
  "age": null,
  "city": null
}

In this example, the "age" and "city" properties have null values. When you parse this JSON string in JavaScript using JSON.parse(), it will become a JavaScript object like the following:

const json = '{"name":"John","age":null,"city":null}';
const obj = JSON.parse(json);

console.log(obj); // { "name": "John", "age": null, "city": null }

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.