Calculator
Calculator
Json
▪ JavaScript Object Notion which is commonly known as JSON is one of the most popular
data transition formats.
▪ The JavaScript Object Notation (JSON) data format enables applications to communicate
over a network, usually through RESTful APIs. REST architecture use for data transfer with
any application and in any format. Before that data transfer using SOAP protocol which
support XML.but XML is very structured files.
▪ All modern languages (e.g., Java, JavaScript, Ruby, C#, PHP, Python, and Groovy) and
platforms provide excellent support for producing (serializing) and consuming
(deserializing) JSON data.
▪ JSON is very simple as it consists of developer-friendly constructs such as Objects, Arrays,
and name/value pairs.
▪ JSON is not limited to Representational State Transfer (REST); it also works with the
following:
o Node.js (which stores project metadata in package.json)
o NoSQL databases such as MongoDB
o Messaging platforms such as Kafka
▪ It is a text-based and lightweight format for data transactions. JSON format was first
computed by Douglas Crockford.
▪ This being a text-based format is easier to read or write by the user and at the same time,
its lightweight property makes it a stress-free alternative for machines to deconstruct or
generate.
Usage of JSON
JSON is mostly used to transfer the data from one system to another. It can transfer data
between two computers, database, programs etc.
▪ It is mainly used for transmitting serialized data over the network connection.
▪ It can be used with all the major programming languages.
▪ It is useful in data transition from the web application to the server.
▪ Most of the web services use JSON based format for data transfer.
JSON Object
▪ JSON object is a set of Keys along with its values (“Key”:value) without any specific
order.
▪ The key and their values are grouped using curly braces, both opening and closing “{
}”.
▪ There are certain rules that need to be followed while creating a JSON structure, we
will learn about those rules while discussing the Key value pairs.
var student={“ROLLNO”:9,“NAME”:”RAJ”,”FEES”:8768.87}
▪ So, in order to create a JSON, the first thing we will need is an attribute. Here, we are
creating an “student” JSON object. Next thing we need is to specify the properties of
the object, let’s assume our student have a “Rollno”, ”Name” and “Fees”. These
properties of the employee are represented as “Keys” in the JSON structure.
▪ Here 9,”RAJ”,8768.87 are value of keys which has different types.
JSON is simple and easier to read and write XML is less readable
JSON doesn’t use end tag In XML, the end tag is mandatory
JSON file is more readable than XML because XML file is big and filled with user-defined tags,
it is short and to the point. thus less-readable
JSON Arrays
▪ Arrays in JSON are similar to the ones that are present in any programming language,
the array in JSON is also an ordered collection of data.
▪ The array starts with a left square bracket “[“and ends with right square bracket “]”.
The values inside the array are separated by a comma.
▪ There are some basic rules that need to be followed if you are going to use an array in
a JSON.
▪ We can store string, number, Boolean, object data in array.
Ex:
▪ JSON array of numbers: It is an ordered list of number values.
Ex: [3,64,62,24]
▪ JSON array of string: It is an ordered list of value with string type. Ex:[“c”,”c++”,”java”]
▪ JSON array of boolean:It is an ordered list of Boolean values.
Ex:[“false”,”true”,”true”,”false”]
▪ JSON array of object:
{ students:[
{“ROLLNO”:9,“NAME”:”RAJ”,”FEES”:8768.87},
{“ROLLNO”:1,“NAME”:”Rina”,”FEES”:8768.87}
{“ROLLNO”:2,“NAME”:”suraj”,”FEES”:8768.87}
]}
• Assign/modify value: arrayname[index].attribute=value
Students[0].NAME=”Raju”
So “Raj” replace by “Raju”
▪ JSON Multidimensional array : It is possible store data array inside another array.
Ex: var students:{“fybca”:[
{“ROLLNO”:9,“NAME”:”RAJ”,”FEES”:8768.87},
{“ROLLNO”:1,“NAME”:”Rina”,”FEES”:8768.87}
{“ROLLNO”:2,“NAME”:”suraj”,”FEES”:8768.87}
],
“Sybca”:[
{“ROLLNO”:90,“NAME”:”RAM”,”FEES”:7777.87},
{“ROLLNO”:18,“NAME”:”Ritu”,”FEES”:9999.87}
{“ROLLNO”:23,“NAME”:”sima”,”FEES”:8768.87}
JSON Comments
▪ JSON doesn't support comments. But as programmers, we are used to add comments
wherever required. We can add using key:value pair.
▪ {“comments”:”this is not required”}
▪ We can add a comment in JSON file included in the JSON object.
Var value = {
“Comment1” : “This is student record”,
_Comment: “This is student record”,
“Name”: "Logan",
“age”: 21,
“Location”: "London"
}
We specify an underscore before comment/in double quote to view it as a comment. But
remember the comment is only a comment in the eyes of the developer but not the
computer.
▪ We can even extract the comment's value from the JSON object.
alert(value._comment);
JSON.parse()
▪ JSON parsing is the process of converting a JSON object in text format (in Quotes) to
a JavaScript object that can be used inside a program.
▪ The JSON.parse() method parses a JSON string, constructing the JavaScript value or
object described by the string.
Example :
<script>
var x = '{"name":"Ram","rno":25}';
var obj = JSON.parse(x);
document.write(obj.name);
</script>
Output : Ram
Example : display data from stud.json file using jquery
Stud.json
[
{"name":"raj","age":34},
{"name":"nila","age":23}
]
Studinfo.html
<html>