Understanding JSON Syntax


JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Note: JSON is not JavaScript; It is a subset of JavaScript.

Syntax

The following example shows the JSON representation of an object that describes a person. The object has string fields for first name and last name, contains an object representing the person’s address, and contains a list of phone numbers (an array).

{
     "firstName": "John",
     "lastName": "Smith",
     "address": {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": 10021
     },
     "phoneNumbers": {
         "home": "212 555-1234",
         "fax": "646 555-4567"
     }
 }

The equivalent for the above in XML:

21 2nd Street New York NY 10021
212 555-1234 212 555-1234

Suppose the above text is contained in the JavaScript string variable contact. Since JSON is a subset of JavaScript’s object literal notation, one can then recreate the object describing John Smith with a simple eval():

   var p = eval("(" + contact + ")");

and the fields p.firstName, p.address.city, p.phoneNumbers[0] etc. are then accessible. The contact variable must be wrapped in parentheses to avoid an ambiguity in JavaScript’s syntax.

In general, eval() should only be used to parse JSON if the source of the JSON-formatted text is completely trusted; the execution of untrusted code is obviously dangerous. JSON parsers are available to process JSON input from less trusted sources.

References:

Related posts:

About Sach

Sachin is a web application developer, technology blogger and web addict! He has over 6 years of web development experience and he writes tutorials primarily focused on LAMP, Ajax, Api's, jQuery etc. He is usability expert and he always likes to share his knowledge with people.

, , ,

  • Muru

    Hi,

    How can i implement disable textfield using json?
    Kindly help me for fixing the issue.

    Many thanks,
    Muru

    • SachinKRaj

      Yeah sure will be able to help you. Could you please provide me the code?

      • Sharath Chowdary

        Hi Sachin,

        The below is the code:

        {
        “Customer Details”: {
        ” Order ID”: {
        “type”: “STRING”,
        “allowNull”: true,
        “noCopy”: true
        },
        “Customer Name”: {
        “type”: “STRING”,
        “allowNull”: true,
        “noCopy”: true
        },
        “Customer ID”: {
        “type”: “STRING”,
        “allowNull”: true,
        “noCopy”: true
        }
        },
        “Contact”: {
        ” Name of the Customer”: {
        “type”: “STRING”,
        “allowNull”: true,
        “noCopy”: true
        },
        “Phone/Mobile”: {
        “type”: “STRING”,
        “allowNull”: true,
        “noCopy”: true
        },
        “Email”: {
        “type”: “STRING”,
        “allowNull”: true,
        “noCopy”: true
        },
        “BitRateUpStream”: {
        “type”: “STRING”,
        “allowNull”: true,
        “noCopy”: true
        },
        “BitRateDownStream”: {
        “type”: “STRING”,
        “allowNull”: true,
        “noCopy”: true
        },
        “Online Storage Space”: {
        “type”: “STRING”,
        “allowNull”: true,
        “noCopy”: true
        },
        “Number of Email Accounts”: {
        “type”: “STRING”,
        “allowNull”: true,
        “noCopy”: true
        }
        },
        “Asset”: {
        “Status”: {
        “type”: “STRING”,
        “picklist”:["Available", "Allocated"]
        },
        “WiMAX Accessory Type”: {
        “type”: “STRING”,
        “picklist”:["Card", "Modem"]
        },
        “Card/Modem No.”: {
        “type”: “INT”,
        “allowNull”: true,
        “noCopy”: true
        }
        },
        “Region Type”: {
        “Type”: {
        “type”: “STRING”,
        “picklist”:["Fire Zone", "Safety Zone", "Room"]
        }
        }
        }

        We want to make a field as read-only. We are unable to achieve it through “hidden” or “invisible”. Kindly guide us. Let us know in case you need further inputs. Thanks in advance.

        • SachinKRaj

          Sharath Chowdary: I just saw your comment, will get it sorted asap and will reply to you here.

          • Sharath Chowdary

            Sure

        • SachinKRaj

          try this property “disabled”: “disabled” in place of hidden or invisible.

          • Sharath Chowdary

            Even “disabled” did not work Sachin!