96

I came across this JSON code. I noticed that it makes use of undefined value. Where can I find more information about this value type?

  tracks:[
     (         {
        codec:"h264",
        language:undefined,
        id:1,
        bitrate:785236,
        content:"video"
     }         ),
     (         {
        codec:"aac",
        language:undefined,
        id:2,
        bitrate:75969,
        content:"audio"
     }         )
  ],
4
  • 6
    Are you sure the JSON is valid? The parentheses (first noted by MuraliPrasanth) look suspicious at least. Commented Dec 10, 2012 at 7:50
  • 1
    That's not valid JSON. Parentheses aren't permitted. Neither is the literal undefined. undefined is a value in javascript: ecma-international.org/ecma-262/5.1/#sec-4.3.9 Commented Nov 6, 2014 at 22:19
  • 2
    are you sure that's JSON? Looks like a snippet of an object literal too me. Commented Jun 8, 2017 at 17:24
  • how can a defined value be undefined? in javascript, any thing is possible ;) Commented Jan 31 at 9:29

2 Answers 2

168
  • undefined is not a valid JSON value, even though it is valid in javascript.

    From the official JSON standard (ECMA-404, Section 5):

    A JSON value can be an object, array, number, string, true, false, or null.

  • For JSON, use null instead of undefined: { "something": null }

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

8 Comments

If you want a value to be undefined, just omit it entirely from the JSON. That way, when you try to read it after parse, it will be undefined.
But that assumes you have full knowledge of what a response should look like. Which is fine if you're building both the server and client, but that is often not the case.
Sure. That isn't an optimal solution, just a workaround for when you (for whatever reason) must return an undefined value.
Good answer, sub-optimal solution. Null and undefined are not the same. If NULL is a valid value in your system then you should be able to communicate both that "Yes, I have a value for that, and it's NULL" and "No, I don't have any value for that". PHP, e.g. suffers from its lack of undefined in its optional parameters, where it's hard (maybe even impossible?) to see whether the caller explicitly passed the default value or didn't pass a value at all. I'd say totymedli's idea is better. If the client doesn't know what to expect from a response, it's not well prepared to handle it either.
Note that "just omitting a value from JSON" is not possible in the case of arrays. JSON.stringify([undefined]), gives you [null], which is not a faithful representation.
|
9

undefined is a special type where it simply indicates that the variable language is not initialized or may be it's not yet defined.

null in javascript simply indicates absence of a value, and it can also be used to indicate “no value” for numbers and strings as well as objects.The undefined value also represents absence of value, it is the value of the variables that have not been initialized and the value when you get from object property or array element that doesn’t exist undefined is a predefined global variable that is initialized to undefined value.

null and undefined doesn’t have any properties or methods.In fact, using . or [] to access property or method of these values causes a TypeError.

4 Comments

Placing a defined value into a variable makes it not undefined. The only option is that an undefined value was stored into that property or that that JSON was not generated automatically.
language-wise, "which is undefined" binds to either the entire previous sentence (if there's a missing comma) or to "language variable", not to "data" (which is further). You have been misunderstood, I believe.
although it would be useful in some cases to be able to say key:undefined in json as a method of implicit key removal when updating data stores. Many times the absence of a key is not the same thing as it having a null value
Lol why is javascript pre-formatted

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.