JSON Object and JSON Schema

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
JavaScript, Third Edition
15-Jul-15 JSON. JSON example “JSON” stands for “JavaScript Object Notation” Despite the name, JSON is a (mostly) language-independent way of specifying.
JSON The Fat Free Alternative to XML. Data Interchange The key idea in Ajax. An alternative to page replacement. Applications delivered as pages. How.
MongoDB An introduction. What is MongoDB? The name Mongo is derived from Humongous To say that MongoDB can handle a humongous amount of data Document.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Data Types.
Copyright © Curt Hill Sounds, Resource Packs, JSON What more would you want?
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
PHP By Sergio Rodriguez By Sergio Rodriguez. PHP G PHP: Hypertext Preprocessor G Scripting language G PHP: Hypertext Preprocessor G Scripting language.
Serialization. Serialization is the process of converting an object into an intermediate format that can be stored (e.g. in a file or transmitted across.
JSON Java Script Object Notation Copyright © 2013 Curt Hill.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
CS346 Javascript -3 Module 3 JavaScript Variables.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Representing data with XML SE-2030 Dr. Mark L. Hornick 1.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
Introduction to JavaScript CSc 2320 Fall 2014 Disclaimer: All words, pictures are adopted from “Simple JavaScript”by Kevin Yank and Cameron Adams and also.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
JavaScript and Ajax (JavaScript Data Types) Week 2 Web site:
Java Script. introduction Today’s web sites need to go much beyond HTML. browsing through a web site, to actually interact with the web site. The web.
OVERVIEW AND PARSING JSON. What is JSON JavaScript Object Notation Used to format data Commonly used in Web as a vehicle to describe data being sent between.
JSON. JSON as an XML Alternative JSON is a light-weight alternative to XML for data- interchange JSON = JavaScript Object Notation It’s really language.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JSON (Copied from and from Prof Da Silva) Week 12 Web site:
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Storing Data.
28 Formatted Output.
11 JavaScript: Objects.
>> Introduction to JavaScript
Chapter 6 JavaScript: Introduction to Scripting
“Under the hood”: Angry Birds Maze
JavaScript is a programming language designed for Web pages.
CSE 3302 Programming Languages
Java for Android is specific
Exporting and Importing Data
JSON Crash Course Traversy Media.
Exporting and Importing Data
Database Systems Week 12 by Zohaib Jan.
Scope, Objects, Strings, Numbers
Consuming Java Script Object Notation (JSON) feeds
JavaScript Object Notation
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
The structure of computer programs
JavaScript an introduction.
Web Systems Development (CSC-215)
Objectives Insert a script element Write JavaScript comments
Built in Fairfield County: Front End Developers Meetup
WEB PROGRAMMING JavaScript.
CHAPTER THREE Sequences.
Introduction to JavaScript
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
2017, Fall Pusan National University Ki-Joune Li
JavaScript What is JavaScript? What can JavaScript do?
JSON for the Data Mortal
Python Primer 1: Types and Operators
JavaScript What is JavaScript? What can JavaScript do?
Chapter 2: Introduction to C++.
Department of Computer Science Cal State East Bay, Hayward, CA
CS 240 – Advanced Programming Concepts
Tutorial 10: Programming with javascript
JavaScript Basics What is JavaScript?
The <script> Tag
Lexical Elements & Operators
Programming Basics Review
Dictionary.
Presentation transcript:

JSON Object and JSON Schema Week 2 - 3

JSON Syntax & Data Types Let's have a quick look at the basic syntax of JSON. it includes the following: Data is represented in name/value pairs Curly braces hold objects and each name is followed by ':' (colon) The name/value pairs are separated by , (comma) Square brackets hold arrays and values are separated by , (comma)

This is a simple example: { "book": [ "id":"01", "language": "Java", "edition": "third", "author": "Herbert Schildt" }, "id":"07", "language": "C++", "edition": "second“, "author": "E.Balagurusamy" }] } This is a simple example:

JSON format supports the following data types:

Number :

Octal and hexadecimal formats are not used Number : It is a double precision floating-point format in JavaScript and it depends on implementation Octal and hexadecimal formats are not used No NaN or Infinity is used in Number Syntax var json-object-name = {"string" : number_value, .......} Example var obj = {"marks": 97}

String :

String : It is a sequence of zero or more double quoted Unicode characters with backslash escaping Character is a single character string i.e. a string with length 1 Syntax var json-object-name = { string : "string value", .......} Example var obj = {"name": "Amit"}

Boolean : It includes true or false values Syntax var json-object-name = { string : true/false, .......} Example var obj = {"name": "Amit", "marks": 97, "distinction": true}

Array : It is an ordered collection of values These are enclosed in square brackets which means that array begins with '[' and ends with ']' The values are separated by , (comma) Array indexing can be started at 0 or 1 Arrays should be used when the key names are sequential integers

Array : Syntax [ value, .......] Example { "books": [ { "language":"Java" , "edition":"second" }, { "language":"C++" , "lastName":"fifth" }, { "language":"C" , "lastName":"third" } ] }

Object : It is an unordered set of name/value pairs Objects are enclosed in curly braces that is, it starts with '{' and ends with '}’ Each name is followed by ':'(colon) and the key/value pairs are separated by , (comma) The keys must be strings and should be different from each other Objects should be used when the key names are arbitrary strings.

Object : Syntax { string : value, .......} Example { "id": "011A", "language": "JAVA", "price": 500 }

Whitespace : It can be inserted between any pair of tokens. It can be added to make a code more readable. Syntax { string : “ ”, .......} Example var obj1 = {"name": "Sachin Tendulkar"} var obj2 = {"name": "SauravGanguly"}

Null : It means empty type Syntax null Example var i = null; if( i==1 ) { document.write("value is 1"); } else { document.write("value is null"); }

JSON Object JSON objects can be created with JavaScript. Let us see the various ways of creating JSON objects using JavaScript: Creation of an empty Object: var JSONObj = {}; Creation of a new Object: var JSONObj = new Object(); Creation of an object with attribute bookname with value in string, attribute price with numeric value. Attribute is accessed by using '.' Operator: var JSONObj = { "bookname ":"VB BLACK BOOK", "price":500 };

This is an example that shows creation of an object in javascript using JSON : (Simple Objects) <html> <head> <title>Creating Object JSON with JavaScript</title> <script language="javascript" > var JSONObj = { "name" : "tutorialspoint.com", "year" : 2005 }; document.write("<h1>JSON with JavaScript example</h1>"); document.write("<br>"); document.write("<h3>Website Name="+JSONObj.name+"</h3>"); document.write("<h3>Year="+JSONObj.year+"</h3>"); </script> </head> <body> </body> </html>

Now let's try to open file using IE or any other javascript enabled browser. It produces the following result:

This is an example that shows creation of an object in javascript using JSON : (Array Objects) <html> <head> <title>Creation of array object in javascript using JSON</title> <script language="javascript" > document.writeln("<h2>JSON array object</h2>"); var books = { "Pascal" : [ { "Name" : "Pascal Made Simple", "price" : 700 }, { "Name" : "Guide to Pascal", "price" : 400 } ], "Scala" : [ { "Name" : "Scala for the Impatient", "price" : 1000 }, { "Name" : "Scala in Depth", "price" : 1300 } ] } var i = 0

document.writeln("<table border='2'><tr>"); for(i=0;i<books.Pascal.length;i++){ document.writeln("<td>"); document.writeln("<table border='1' width=100 >"); document.writeln("<tr><td><b>Name</b></td><td width=50>" + books.Pascal[i].Name+"</td></tr>"); document.writeln("<tr><td><b>Price</b></td><td width=50>" + books.Pascal[i].price +"</td></tr>"); document.writeln("</table>"); document.writeln("</td>"); } for(i=0;i<books.Scala.length;i++){

+ books.Scala[i].Name+"</td></tr>"); document.writeln("<tr><td><b>Price</b></td><td width=50>" + books.Scala[i].price+"</td></tr>"); document.writeln("</table>"); document.writeln("</td>"); } document.writeln("</tr></table>"); </script> </head> <body> </body> </html>

Now let's try to open file using IE or any other javascript enabled browser. It produces the following result:

JSON Schema JSON Schema is a specification for JSON based format for defining the structure of JSON data. It was written under IETF draft which expired in 2011. JSON Schema: Describes your existing data format Clear, human- and machine-readable documentation Complete structural validation, useful for automated testing Complete structural validation, validating client-submitted data

JSON Schema Validation Libraries There are several validators currently available for different programming languages. Currently the most complete and compliant JSON Schema validator available is JSV

Example Given below is a basic JSON schema, which covers a classical product catalog description: { "$schema": "http://json-schema.org/draft-04/schema#", "title": "Product", "description": "A product from Acme's catalog", "type": "object", "properties": { "id": { "description": "The unique identifier for a product", "type": "integer" }, "name": { "description": "Name of the product", "type": "string" "price": { "type": "number", "minimum": 0, "exclusiveMinimum": true } "required": ["id", "name", "price"]