Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript JSON Objects

JS JSON Objects

JSON object literals are surrounded by curly braces {}.

JSON Object Literals

JSON object literals are surrounded by curly braces {}.

JSON object literals contains key/value pairs.

Keys and values are separated by a colon.

Keys must be strings, and values must be a valid JSON data type:

  • string
  • number
  • object
  • array
  • boolean
  • null

Each key/value pair is separated by a comma.

					 
        
          // This is a JSON string
          '{"name":"John", "age":30, "car":null}'

          // Inside the JSON string there is a JSON object literal
          {"name":"John", "age":30, "car":null}
        
      

It is a common mistake to call a JSON object literal "a JSON object".

JSON cannot be an object. JSON is a string format.

The data is only JSON when it is in a string format. When it is converted to a JavaScript variable, it becomes a JavaScript object.

JavaScript Objects

You can create a JavaScript object from a JSON object literal by parsing a JSON string.

					 
        
          myJSON = '{"name":"John", "age":30, "car":null}';
          myObj = JSON.parse(myJSON);
        
      
Accessing Object Values

You can access object values by using dot (.) notation.

You can also access object values by using bracket ([]) notation.

					 
        
          const myJSON = '{"name":"John", "age":30, "car":null}';
          const myObj = JSON.parse(myJSON);
          x = myObj.name;
          y = myObj["name"];
        
      
Looping an Object

You can loop through object properties with a for-in loop.

					 
        
          const myJSON = '{"name":"John", "age":30, "car":null}';
          const myObj = JSON.parse(myJSON);

          let text = "";
          for (const x in myObj) {
            text += x + ", ";
          }
        
      

In a for-in loop, use the bracket notation to access the property values.

					 
        
          const myJSON = '{"name":"John", "age":30, "car":null}';
          const myObj = JSON.parse(myJSON);

          let text = "";
          for (const x in myObj) {
            text += myObj[x] + ", ";
          }
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved