Introduction to JSON

  • json

JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging. also a subset of JavaScript’s Object Notation (the way objects are built in JavaScript). JSON is used as web services responses. In the ‘old’ days, web services used XML as their primary data format for transmitting back data, but since JSON appeared, it has been the preferred format because it is much more a lightweight.

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

JSON values can consist of :

objects (collections of name-value pairs), arrays (ordered lists of values), strings (in double quotes), numbers,c true, false, or null. JSON is language independent.

JSON with PHP

After PHP Version 5.2.0, JSON extension is decodes and encodes functionalities as default. Json_encode – returns the JSON representation of values Json_decode – Decodes the JSON String Json_last_error – Returns the last error occured.

JSON Syntax and 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 in JavaScript

JSON, is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. It is the primary data format used for asynchronous browser/server communication (AJAX), largely replacing XML.
Although originally derived from the JavaScript scripting language, JSON is a language-independent data format. Code for parsing and generating JSON data is readily available in many programming languages.
The JSON format was originally specified by Douglas Crockford. It is currently described by two competing standards, RFC 7159 and ECMA-404. The ECMA standard is minimal, describing only the allowed grammar syntax, whereas the RFC also provides some semantic and security considerations.The official Internet media type for JSON is application/JSON. The JSON filename extension is .json.

Here is the description of all the parameters used by this method −

  • url− A string containing the URL to which the request is sent.
  • data− This optional parameter represents key/value pairs that will be sent to the server.
  • callback − This optional parameter represents a function to be executed whenever the data is loaded successfully.

IMPORTANCE OF JSON

With the rise of AJAX-powered sites, it’s becoming more and more important for sites to be able to load data quickly and asynchronously, or in the background without delaying page rendering. JSON allows us to overcome the cross-domain issue because we can use a method called JSONP that uses a callback function to send the JSON data back to our domain. It’s this capability that makes JSON so incredibly useful, as it opens up a lot of doors that were previously difficult to work around.

Some facts about JSON

  • JSON is limited to text and numeric values.
  • It doesn’t support binary data.
  • An JSON object is a set of key / name pairs which starts with “{” and ends with “}”.

JSON is built on two structures :

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
  • These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.

JSON format :

The below shows the format of json data:

[‘abc’,’dg’,’erte’]
// 0 1 2 <------- Index ( not 1 2 3 ).

JSON vs XML :

Simplicity

XML is simpler than SGML, but JSON is much simpler than XML. JSON has a much smaller grammar and maps more directly onto the data structures used in modern programming languages.

Extensibility

JSON is not extensible because it does not need to be. JSON is not a document markup language, so it is not necessary to define new tags or attributes to represent data in it.

Interoperability

JSON has the same interoperability potential as XML.