Author - StudySection Post Views - 169 views
JSON

JSON Introduction

JSON (JavaScript Object Notation) is a format that is used to store the data and transport it to the web pages over the server.

JSON : JavaScript Object Notation

The JSON format is only text but the syntax for JSON is derived from JavaScript Object Notation. So code for understanding and writing JSON can be written in any programming language as JSON is language independent.

Example

This JSON syntax defines a students object: an array of 2 student records (objects):
{
"students":[
{"firstName":"shruti", "lastName":"arora"},
{"firstName":"priyal", "lastName":"jain"}
] }

JSON Syntax Rules

  • Data is in name/value pairs
  • Pairs must be separated by commas
  • Objects must be in curly braces
  • Arrays must be in the square brackets container.

JSON Data – A Name and a Value
Just like JavaScript object properties, 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:
In JSON name must be in double-quotes.
"firstName":"shruti"
JavaScript:
firstName:”shruti”
In JSON string values must be written in double quotes.
"firstName":"shruti"
But in javascript, it can be written in double or single quotes.
firstName:"shruti"
firstName:’shruti’

In JSON, values 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

Objects

JSON objects are written inside curly braces.
Objects can contain multiple name/value pairs, Just like in JavaScript
{"firstName":"shruti", "lastName":"arora"}

JSON Arrays
Inside square brackets, we write JSON arrays .
Just like in JavaScript, an array can contain objects:
"students":[
{"firstName":"shruti", "lastName":"arora"},
{"firstName":"priyal", "lastName":"jain"}
]

In the example above, the object “students” is an array. It contains two objects.
Each object is a record of a student with a first name and the last name.

Converting a JSON Text to a JavaScript Object
A common use of JSON is to read data from a web server and display the data on a web page.
For simplicity, this can be demonstrated using a string as input.
First, create a JavaScript string containing JSON syntax:
var text = '{ "students" : [' +
'{ "firstName":"shruti" , "lastName":"arora" },' +
'{ "firstName":"priyal" , "lastName":"jain" } ]}';

JSON.parse() is a JavaScript built-in function to convert the string into a javascript object
var obj = JSON.parse(text);

Sending Data
If you have a JavaScript object, you can convert the object into JSON, and send it to a server:

Example:
var jsObj= {name: "shruti", age:24, city: "Mohali"};
var jsonObj= JSON.stringify(jsObj);
window.location = "demoJson.php?x=" +jsonObj;

Receiving Data
If you receive data in JSON format, you can convert it into a JavaScript object:

Example:
var jsonObj= '{"name":"shruti", "age":24, "city":" Mohali"}';
var myObj = JSON.parse(jsonObj);

To handle JSON, PHP has some built-in functions.
PHP built-in function json_encode() can be used to convert an object into JSON

PHP file
<?php
$myObj->name = "Shruti";
$myObj->age =24;
$myObj->city = "MOhali";
$myJSON = json_encode($myObj);
echo $myJSON;
?>

Output:
{"name":"Shruti","age":24,"city":"Mohali"}

Get certification for your knowledge in the fundamentals of Computer functioning by clearing the Computer Certification Exam conducted by StudySection. After going through this Computer Certification exam, you will be able to evaluate your basic knowledge of computers.

Leave a Reply

Your email address will not be published.