Introduction to JSON

JSON stands for JavaScript Object Notation. JSON is an open standard format that transmits data using human-readable text in the form of attribute–value pairs. Although originally derived from the JavaScript scripting language, JSON is a language-independent data format, and code for parsing and generating JSON data is readily available in a large variety of programming languages including Java.

JSON is currently described by two competing standards, RFC 7159 and ECMA-404. The official Internet media type for JSON is application/json. If this MIME type header is not sent across the browser, it treats the incoming JSON as plain text. The JSON filename extension is .json.

JSON is basically built on two structures:

  • Object, which is an unordered set of name/value pairs.    
  • Array, which is an ordered collection of values.

All modern programming languages like Java support them in one form or another.

JSON syntax for different types of data structures like object, array, and values are as follows:

  • An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
  • An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
  • A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested. A string is wrapped in double quotes, using backslash escapes. The octal and hexadecimal formats are not used for a number. Whitespace can be inserted between any pair of tokens.

Example

{

  "name": "Heartin",

  "age":29,

  "skill": ["java", "javaee"]

}

 

JSON in JavaScript

JSON can be considered a subset of the object literal notation of JavaScript and a JSON object can be created similar to how we create a JavaScript object using curly braces:

<script type="text/javascript">

var emp = {

  "name": "Heartin",

  "age":29,

  "skill": ["java", "javaee"]

};

</script>

Few notable differences from a normal JavaScript object are:

  • Since a JSON key is a string, we can use any valid string for a key such as spaces, special characters, and hyphens, which is not valid in a normal JavaScript object.
  • A JavaScript object can carry functions within, while a JSON object cannot carry any functions. JSON is used as a data interchange format whereas JavaScript is much more with the ability to make the page dynamic. JSON can be considered a subset of the object literal notation of JavaScript.

 

JSON in Java

There are many JSON libraries available in java and the most popular seems to be Jackson and Google GSon. Understanding the importance of JSON, Java EE has also included support for JSON through its APIs in Java EE 7.

Java EE 7 provides two types of JSON APIs: a streaming event based API and object model API. The streaming event based API (JsonParser) is based on Stax and is useful for large JSON documents. The object model API (JsonObject) is DOM based and stores everything in memory, and is useful when you have to navigate between different nodes of the JSON document.

Java EE 7 also provides support for HTML 5 WebSockets which can be used effectively to transfer JSON data between client and server bidirectional.

 

Reference

http://www.json.org/

http://en.wikipedia.org/wiki/JSON

Developing RESTful Services with JAX-RS 2.0,WebSockets, and JSON by Masoud Kalali and Bhakti Mehta.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)