So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.

Similar documents
Introduction to JSON. Roger Lacroix MQ Technical Conference v

JSON is a light-weight alternative to XML for data-interchange JSON = JavaScript Object Notation

CSC Web Technologies, Spring Web Data Exchange Formats

JSON as an XML Alternative. JSON is a light-weight alternative to XML for datainterchange

Session 14. Serialization/JSON. Lecture Objectives

AJAX ASYNCHRONOUS JAVASCRIPT AND XML. Laura Farinetti - DAUIN

CSC 337. JavaScript Object Notation (JSON) Rick Mercer

AJAX. Ajax: Asynchronous JavaScript and XML *

NOSQL EGCO321 DATABASE SYSTEMS KANAT POOLSAWASD DEPARTMENT OF COMPUTER ENGINEERING MAHIDOL UNIVERSITY

COMP9321 Web Application Engineering

/ Introduction to XML

Assigns a number to 110,000 letters/glyphs U+0041 is an A U+0062 is an a. U+00A9 is a copyright symbol U+0F03 is an


Grading for Assignment #1

This tutorial will help you understand JSON and its use within various programming languages such as PHP, PERL, Python, Ruby, Java, etc.

User Interaction: XML and JSON

IBM Informix xC2 Enhancements IBM Corporation

[301] JSON. Tyler Caraza-Harter

AJAX. Lab. de Bases de Dados e Aplicações Web MIEIC, FEUP 2010/11. Sérgio Nunes

Kyle Rainville Littleton Coin Company

Web applications design

Event : Common Europe Speaker : Koen Decorte CD-Invest nv

Manju Muralidharan Priya. CS4PM Web Aesthetics and Development WEEK 11

CSCE 156 Computer Science II

markup language carry data define your own tags self-descriptive W3C Recommendation

Firebase Realtime Database. Landon Cox April 6, 2017

10/18/2017. Announcements. NoSQL Motivation. NoSQL. Serverless Architecture. What is the Problem? Database Systems CSE 414

JavaScript Lecture 1

16. Objects & JSON. Dr. Dave Parker. Informa;on and the Web, 2014/15

Princess Nourah bint Abdulrahman University. Computer Sciences Department

Making a Clickable Map Display

10.1 Overview of Ajax

XML JavaScript Object Notation JSON Cookies Miscellaneous What Javascript can t do. OOP Concepts of JS

IN Development in Platform Ecosystems Lecture 3: json, ajax, APIs

JSON - Overview JSon Terminology

Progettazione e Produzione Multimediale. Javascript

Working with Database. Client-server sides AJAX JSON Data formats Working with JSON data Request Response Bytes Database

Copyright Descriptor Systems, Course materials may not be reproduced in whole or in part without prior written consent of Joel Barnum

JSON Support for Junos OS

Flat triples approach to RDF graphs in JSON

ASP.NET AJAX adds Asynchronous JavaScript and XML. ASP.NET AJAX was up until the fall of 2006 was known by the code-known of Atlas.

Lecture 8 (7.5?): Javascript

Cloud-based Database Systems (No SQL Databases) Reference: Chapter 19.9, Database Systems Concepts, Silberschatz, et al.

Processing XML and JSON in Python

XML Processing & Web Services. Husni Husni.trunojoyo.ac.id

CSE 344 APRIL 16 TH SEMI-STRUCTURED DATA

5/2/16. Announcements. NoSQL Motivation. The New Hipster: NoSQL. Serverless. What is the Problem? Database Systems CSE 414

SEEM4570 System Design and Implementation Lecture 03 JavaScript

CGS 3066: Spring 2015 JavaScript Reference

Database Systems CSE 414

Working with JavaScript

INLEDANDE WEBBPROGRAMMERING MED JAVASCRIPT INTRODUCTION TO WEB PROGRAMING USING JAVASCRIPT

JSON. Dr. Kanda Runapongsa Saikaew Computer Engineering Department

CONTRACTOR IS ACTING UNDER A FRAMEWORK CONTRACT CONCLUDED WITH THE COMMISSION

REST. Web-based APIs

BEFORE CLASS. If you haven t already installed the Firebug extension for Firefox, download it now from

6 7. APRIL ATLANTA, GA, USA. Building for the future. Better, faster, everywhere. Building for the future. Better, faster, everywhere.

HTML 5 and CSS 3, Illustrated Complete. Unit L: Programming Web Pages with JavaScript

XMLHttpRequest. CS144: Web Applications

Announcements. Two Classes of Database Applications. Class Overview. NoSQL Motivation. RDBMS Review: Serverless

AJAX and JSON. Day 8

COMS 469: Interactive Media II

Introduction to Web Development

Assignment: Seminole Movie Connection

5/1/17. Announcements. NoSQL Motivation. NoSQL. Serverless Architecture. What is the Problem? Database Systems CSE 414

Introduction to XML. Asst. Prof. Dr. Kanda Runapongsa Saikaew Dept. of Computer Engineering Khon Kaen University

XML. Jonathan Geisler. April 18, 2008

Class Overview. Two Classes of Database Applications. NoSQL Motivation. RDBMS Review: Client-Server. RDBMS Review: Serverless

TIME SCHEDULE MODULE TOPICS PERIODS. HTML Document Object Model (DOM) and javascript Object Notation (JSON)

Assignment #3 CSCI 201 Spring % of course grade Title Weathermeister Back-End API Integration

Processing and Creating JSON from RPG

External data representation

Semistructured Data and XML

Introduction to XML 3/14/12. Introduction to XML

"a computer programming language commonly used to create interactive effects within web browsers." If actors and lines are the content/html......

Lesson 12: JavaScript and AJAX

Intro to XML. Borrowed, with author s permission, from:

Packaging Data for the Web

PHP. Interactive Web Systems

AJAX: Introduction CISC 282 November 27, 2018

2/6/2012. Rich Internet Applications. What is Ajax? Defining AJAX. Asynchronous JavaScript and XML Term coined in 2005 by Jesse James Garrett

User Interaction: jquery

Client Side JavaScript and AJAX

CS312: Programming Languages. Lecture 21: JavaScript

JavaScript: The Basics

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Why Discuss JavaScript? CS312: Programming Languages. Lecture 21: JavaScript. JavaScript Target. What s a Scripting Language?

Variables and Typing

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

jquery and AJAX

.. Cal Poly CPE/CSC 366: Database Modeling, Design and Implementation Alexander Dekhtyar..

CSE 344 JULY 9 TH NOSQL

a Very Short Introduction to AngularJS

PROCESSING NON-XML SOURCES AS XML. XML Amsterdam Alain Couthures - agencexml 06/11/2015

Multimedia im Netz Online Multimedia Winter semester 2015/16. Tutorial 07 Minor Subject

Chapter 1 - Consuming REST Web Services in Angular

IAT 355 : Lab 01. Web Basics

CSC Web Programming. Introduction to JavaScript

Intro To Javascript. Intro to Web Development

The basic format structure of the HTTP request / response messages are similar and consist of the following:

Transcription:

What is JSON? JSON stands for JavaScript Object Notation JSON is a lightweight data-interchange format JSON is "self-describing" and easy to understand JSON is language independent * JSON uses JavaScript syntax, but the JSON format is text only. Text can be read and used as a data format by any programming language. Why use JSON? Since the JSON format is text only, it can easily be sent to and from a server, and used as a data format by any programming language. JavaScript has a built in function to convert a string, written in JSON format, into native JavaScript objects: JSON.parse() So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object. http://www.w3schools.com/js/js_json_syntax.asp 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 A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value: "name":"john" 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 { "name":"john" } In JavaScript, keys can be strings, numbers, or identifier names:

JavaScript { name:"john" } JSON Values 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 In JSON, string values must be written with double quotes: 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 person = { "name":"john", "age":31, "city":"new York" }; You can access a JavaScript object like this: // returns John person.name; It can also be accessed like this: // returns John person["name"]; Data can be modified like this: person.name = "Gilbert"; It can also be modified like this: person["name"] = "Gilbert"; The file type for JSON files is ".json" The MIME type for JSON text is "application/json

Exchanging Data When exchanging data between a browser and a server, the data can only be text. JSON is text, and we can convert any JavaScript object into JSON, and send JSON to the server. We can also convert any JSON received from the server into JavaScript objects. This way we can work with the data as JavaScript objects, with no complicated parsing and translations. Sending Data If you have data stored in a JavaScript object, you can convert the object into JSON, and send it to a server: ex <!DOCTYPE html> <html> <body> <h2>convert a JavaScript object into a JSON string, and send it to the server.</h2> <script> var myobj = { "name":"john", "age":31, "city":"new York" }; var myjson = JSON.stringify(myObj); window.location = "demo_json.php?x=" + myjson; </script> </body> </html> output on the screen is demo_json.php: John from New York is 31 Receiving Data If you receive data in JSON format, you can convert it into a JavaScript object: var myjson = '{ "name":"john", "age":31, "city":"new York" }'; var myobj = JSON.parse(myJSON); document.getelementbyid("demo").innerhtml = myobj.name; Storing Data When storing data, the data has to be a certain format, and regardless of were you choose to store it, text is always one of the legal formats. JSON makes it possible to store JavaScript objects as text. Storing data in local storage

//Storing data: myobj = { "name":"john", "age":31, "city":"new York" }; myjson = JSON.stringify(myObj); localstorage.setitem("testjson", myjson); //Retrieving data: text = localstorage.getitem("testjson"); obj = JSON.parse(text); document.getelementbyid("demo").innerhtml = obj.name;

JSON vs XML Both JSON and XML can be used to receive data from a web server. The following JSON and XML examples both defines an employees object, with an array of 3 employees: JSON Example {"employees":[ { "firstname":"john", "lastname":"doe" }, { "firstname":"anna", "lastname":"smith" }, { "firstname":"peter", "lastname":"jones" } ]} XML Example <employees> <employee> <firstname>john</firstname> <lastname>doe</lastname> </employee> <employee> <firstname>anna</firstname> <lastname>smith</lastname> </employee> <employee> <firstname>peter</firstname> <lastname>jones</lastname> </employee> </employees> JSON is Like XML Because Both JSON and XML are "self describing" (human readable) Both JSON and XML are hierarchical (values within values) Both JSON and XML can be parsed and used by lots of programming languages Both JSON and XML can be fetched with an XMLHttpRequest JSON is Unlike XML Because JSON doesn't use end tag JSON is shorter JSON is quicker to read and write JSON can use arrays The biggest difference is: XML has to be parsed with an XML parser. JSON can be parsed by a standard JavaScript function. Why JSON is Better Than XML XML is much more difficult to parse than JSON. JSON is parsed into a ready-to-use JavaScript object. For AJAX applications, JSON is faster and easier than XML:

1. 2. 3. 1. 2. Using XML Fetch an XML document Use the XML DOM to loop through the document Extract values and store in variables Using JSON Fetch a JSON string JSON.Parse the JSON string