Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON undefined value type

Tags:

json

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"      }         )   ], 
like image 717
Zombo Avatar asked Dec 10 '12 07:12

Zombo


People also ask

Does JSON allow undefined value?

undefined , Function s, and Symbol s are not valid JSON values. If any such values are encountered during conversion they are either omitted (when found in an object) or changed to null (when found in an array).

Is undefined a data type in JSON?

Undefined is not a valid JSON type. All javascript objects are not valid JSON and vice versa. All other values will be sent to the response but undefined won't be sent.

Why is my JSON data undefined?

Description. The JSON-safe values are converted to their corresponding JSON string form. The JSON-unsafe values on the other hand return : undefined if they are passed as values to the method.

How do I check if a JSON object is undefined?

You want to be checking for undefined , not null . If you want to cover for all falsy values (null, undefined, zero...) you may just turn the first line into : if (this. ItemAttributes.


2 Answers

  • 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 }

like image 134
Ringo Avatar answered Sep 24 '22 10:09

Ringo


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.

like image 37
Murali N Avatar answered Sep 22 '22 10:09

Murali N