What Is JSON Syntax Explain JSON Syntax Rules With Examples

admin_img Posted By Bajarangi soft , Posted On 08-10-2020

In JSON syntax is a subset of the JavaScript syntax.

What Is JSON Syntax Explain JSON Syntax Rules With Examples

JSON Syntax Rules

JSON syntax is derived from JavaScript object notation syntax:

  • Data is in name/value pairs

  • Data is separated by commas

  • Curly braces hold objects

  • Square brackets hold arrays


JSON Data - A Name and a Value

JSON data is written as name/value pairs.

A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value:

Company_name: "Bajarangisoft"

JSON names require double quotes. JavaScript names don't.
 

JSON - Evaluates to JavaScript Objects

The JSON format is almost identical to JavaScript objects.

In JSON, keys must be strings, written with double quotes:

JSON

{ Company_name: "Bajarangisoft"}


In JavaScript, keys can be strings, numbers, or identifier names:

JavaScript
 

{ Company_name: "Bajarangisoft"}
 

JSON Values

 

In JSONvalues must be one of the following data types:

  • a string
  • a number
  • an object (JSON object)
  • an array
  • a boolean
  • null

In JavaScript values can be all of the above, plus any other valid JavaScript expression, including:

  • a function
  • a date
  • undefined

In JSON, string values must be written with double quotes:

JSON

{ "Company_name":"Bajarangisoft" }
 

In JavaScript, you can write string values with double or single quotes:
 

JavaScript
{ "Company_name":"Bajarangisoft" }
 

JSON Uses JavaScript Syntax

Because JSON syntax is derived from JavaScript object notation, very little extra software is needed to work with JSON within JavaScript.

With JavaScript you can create an object and assign data to it, like this:

var company = { Company_name: "Bajarangisoft", since: 2, city: "Banglore" };

You can access a JavaScript object like this:

Example(1)

// returns John
person.name;

Example(2)
It can also be accessed like this:
// returns John
person["name"];

Example(3)
Data can be modified like this:
person.name = "Gilbert";

Example(4)

It can also be modified like this:

person["name"] = "Gilbert";
 

JavaScript Arrays as JSON

The same way JavaScript objects can be used as JSON, JavaScript arrays can also be used as JSON.

 


JSON Files

  • The file type for JSON files is ".json"

  • The MIME type for JSON text is "application/json"

 


Complete Code For JSON Syntax.

<!DOCTYPE html>
<html>
<head>
    <title>What Is JSON Syntax Explain JSON Syntax Rules With Examples</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <br>
    <div class="text-center">
        <h1 id="color" style="color: tomato">What Is JSON Syntax Explain JSON Syntax Rules With Examples</h1>
    </div>
    <br>
    <div class="well">
        <h2 id="demo1"></h2>
        <h2 id="demo2"></h2>
        <h2 id="demo3"></h2>
        <h2 id="demo4"></h2>
        <h2 id="demo5"></h2>
    </div>
    <script>
        //example1
        var myObj, x;
        myObj = {name: "Kiran", age: 30, city: "New York"};
        x = myObj.name;
        document.getElementById("demo1").innerHTML = x;
        //example2
        var myObj1, x1;
        myObj1 = {name: "Shiva", age: 20, city: "Delhi"};
        myObj1["name"] = "Gilbert";
        document.getElementById("demo2").innerHTML = myObj1.name;
        //example 3
        var myObj2, x;
        myObj2 = {name: "John", age: 30, city: "New York"};
        x = myObj2.name;
        document.getElementById("demo3").innerHTML = x;
        //example4
        myObj3 = {name: "Priya", age: 30, city: "New York"};
        x = myObj3["name"];
        document.getElementById("demo4").innerHTML = x;
    </script>
</div>
</body>
</html>

 

Related Post