top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

JSON Quick Reference

+1 vote
334 views

                                                                                                         

Uses of JSON

JSON is widely preferred to serialize and send data over a network, for instance, between a web application and a web server. Serialization transforms data structures to a format that is appropriate for transferring over a network or for storing in a memory buffer or file for later retrieval.

JSON, as a data-interchange format, it also used to facilitate communication between incompatible platform. For instance, it becomes possible for a Java application on UNIX to send information to visual Basic application running on windows by using JSON.

When XML was introduced during the late 90s, it was a boon for several business mangers due to its intuitive HTML -like look . On the other hand, developers knew that parsing XML is not simple.

JSON is also preferred for developing JavaScript based applications, such as web sites and browser extensions. Even Application Programming Interfaces (APIs) and web services uses JSON to send public data.

History of JSON

JSON was introduced to fulfil the requirement of real-time, stateful exchange of data between a server and client but without plugins such as java applets. While Douglas Crockford originally specified this new format, the term was first coined at the state software company started by co-founders namely, Rebert F, Chip Morningstar, and Crockford.

The co-founders decided to develop a system that can harness the normal browser potential as well as offer an abstraction layer for designing stateful applications for the web. These applications should maintain a persistent connection with the server by having two Hypertext Transfer Protocol (HTTP) connections open. These connections should be recyclable prior to the typical timeout of the browser, in case of no further exchange of data. One of the co-founders Morningstar developed the notion for such a state application framework.

Features of JSON

  • Standard Structure: The JSON format has a standard structure, making it easier for developers to write code.
  • Simplicity: The representation of data in JSON format is simple, which makes it simpler for a developer to comprehend the code. Even parsing becomes easy, regardless of the program language in use. The compact and simple JSON format allows quick transfer of data across platforms for reaching a variety of users across the globe.
  • Open: JSON is similar to XML in terms of openness. It is publicly available and is not at the core of corporate/political tussles for standardization. One can easily take advantage of open structure for adding other elements when needed and adapt the system to hold new industry-specific terms. If these terms are in XML format, one can automatically convert them to JSON, as migrating is straightforward.
  • simplicity: The representation of data in JSON format is simple, which makes it simpler for a developer to comprehend the code. Even parsing becomes easy, regardless of the program language in use. The compact and simple JSON format allows quick transfer of data across platforms for reaching a variety of users across the globe.
  • Open: JSON is similar to XML in terms of openness. It is publicly available and is not at the core of corporate/political tussles for standardization. One can easily take advantage of open structure for adding other elements when needed and adapt the system to hold new industry-specific terms.
  • Self-describing: JSON is self-describing, which means it is possible to describe one’s own field names and their specific values. This enables applications to manipulate JSON data.
  • Lightweight: Data in JSON format does not much storage space. It is also easier to obtain and load such data quickly as well asynchronously without reloading the page. This makes AJAX applications efficient in terms of performance.
  • Clean Data: With a standardized structure, JSON is less susceptible to errors. The application obtaining JSON data is aware of how to get the data and what to expect, which is different from using HTML. Such a structure also helps the developer in offering robust cross-browser solutions, which is useful considering the advent of different browser on mobile devices.
  • Efficiency: While using JSON data structure, the previous outputted data list is created or cloned on the client side. This means that only the changed information is transferred to client on every request. The small optimization is likely to boost both performance and usability of web applications, considering the swift increase in internet connections.
  • Extensibility: JSON allows storing only standard data such as numbers and text. On the other hand, XML allows storing any data type. While this is a benefit of using XML, it is also a con or a drawback, as it makes reading a bit more difficult.
  • Internationalization: JSON implements the Unicode norm due to which generic tools can easily manipulate its structured data. Thus, this structured data is easy to distribute to a wide range of platforms as well as users.

JSON vs. XML

Characteristic

JSON

XML

Data Types

Offers scalar data types and articulates structured data in the form of objects and arrays.

Does not offer any idea of data types due to which replying on XML Schema is essential for specifying information about the data types.

Array Support

Provides native support

Expresses arrays by conventions. For instance, XML employs an outer placeholder element that forms that content in an array as inner elements.

Object Support

Provides native support

Expresses objects by conventions, usually by combining attributes and elements.

Null support

Recognizes the value natively

Mandates the use of xsi:nil on elements in an instance document along with the import of the related namespace.

Comments

Does not support

Provides native support (via APIs).

Namespaces

Does not support namespaces and that naming collisions do not occur, as objects are nested or the object member name has a prefix.

Accepts namespaces to prevent name collisions and safely extend the prevalent XML standards.

Formatting

Is simple and offers more direct data mapping.

Is complex and needs more effort for mapping application types to elements.

Size

Has very short syntax, which gives formatted text wherein most space is taken up by the represented data.

Has lengthy documents, particularly in case of element-centric formatting.

Parsing in JavaScript

Needs no additional application for parsing (JavaScript’s eval() function works well).

Implements XML DOM and requires extra code for mapping text to JavaScript objects.

Parsing

Has JSONPath for selecting specific sections of the data structure but is not widely used.

Has XPath specification for doing the same in an XML document and is widely used.

Styling

Has no special specification

Has XSLT specification for styling an XML document

Security

Is less secure, as the browser has no JSON parser

Is more secure.

 

Let us now see how XML and JSON format appears while storing the details of three students. These details are stored in a text-based format for later retrieval.

Code snippet 1: shows the XML code.

<students>

<student>

<name>Pankaj</name> <age>24</age> <city> Bangalore </city>

</student>

<student>

<name>Bob</name> <age>22</age> <city> Washington </city>

</student>

<student>

<name>Michal</name> <age>27</age> <city> Denver</city>

</student>

</students>

Code snippet 2:  Shows the JSON code.

{“students”: [

{“name”: “Pankaj”, “age”: “24”, “city”: “Bangalore”},

{“name”: “Bob”, “age”: “22”, “city”: “Washington”},

{“name”: “Michal”, “age”: “27”, “city”: “Denver”}

] }

Note: It is clear that JSON is more lightweight as well as compact than XML. 

posted Jun 26, 2017 by Sonali Gupta

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

                                                                                                                                     

JSON vs. XML

Both JSON and XML are capable of representing in-memory data in a readable textual format. Similarly, both of them are isomorphic, which means an equivalent one of the given piece of text is possible in the other format. For instance, while invoking a public Web service, a developer can state in the query string parameter whether the output should be in XML or JSON format.

Due to such similarities, it might not be simple to choose a suitable data exchange format from the two. This is where it is essential to consider the differentiating characteristics or both the formats and find out which one is more suitable for a particular application.

Compares the major characteristics of both formats.

Characteristic

JSON

XML

Data Types

Offers scalar data types and articulates structured data in the form of objects and arrays

  Does not offer any idea of data types due to which relying on XML Schema is essential for specifying information about the data types.

Array Support

Provides native support

  Expresses array by conventions. For instance, XML employs an outer placeholder element that forms the content in an array as inner elements.

Object Support

Provides native support

  Expresses objects by conventions, usually by combining attributes and elements.

Null Support

Recognizes the value natively.

  Mandates the use of xsi:nil on elements in an instance document along with the import of the related namespace.

Comments

Does not support

  Provides native support (via APIs).

Namespaces

Does not support namespaces and that naming collisions do not occur, as objects are nested or the object member name has a prefix.

  Accepts namespaces to prevent name collisions and safely extent the prevalent XML standards.

Formatting

Is simple and offers more direct data mapping

  It complex and needs more effort for mapping application types to elements.

Size

Has very short syntax, which gives formatted text wherein most space is taken up by the represented data.

  Has lengthy documents, particularly in case of element-centric formatting.

Parsing in JavaScript

Has very short syntax, which gives formatted text wherein most space is taken up by the represented data.

  Has lengthy documents, particularly in case of element-centric formatting.

Parsing in JavaScript

Needs no additional application for parsing (JavaScript’s eval() function work well ).

  Implements XML DOM and requires extra code for mapping text to JavaScript objects.

Parsing

Has JSONPath for selecting specific sections of the data structure but is not widely used.

  Has XPath specification for doing the same in an XML document and is widely used.

 

Learning Curve

Is not sleep at all, due to familiarity with the structure and the underlying dynamic programming language.

  Is steep with the need to know several technologies and concepts such as XPath, XSL Transformations (XSLT), DOM, Schema, and Namespaces.

Complexity

Is complex

  Is more complex.

Schema (Metadata)

Has a schema but is not widely used.

  Used many specifications    for defining a schema,        including XML Schema      Definition (XSD) and Document Type Definition (DTD)

Styling

Has no special specification.

  Has XSLT specification for  styling an XML document.

Security

Is less secure, as the browser has no JSON parser.

  Is more secure.

READ MORE

Comparison of JSON with Relational Databases

Although even JSON hosts or represents data, it differs significantly from the conventional relational database model implemented in RDBMS applications such as SQL Server and MySQL. Knowing these differences can help you to prefer JSON over RDBMS or vice-versa, as  per the data structure and type.

Following are the basic differences:.

  • Structure: A table stores data in a relational database, while JSON uses arrays and objects from the same, which can be recursively nested.
  • Metadata: A schema stores data about the type as well as structure of the stored data in a relational database. Further, schemas are created while creating a database and tables, not at the time of storing data. Even JSON can use a schema but it is not pre-created like in a relational database. Usually, a JSON structure is self-describing but if it uses a schema, it ensures more flexibility than a schema in a relational in a relational atabase.
  • Data Retrieval:  A relational database utilizes SQL, which is a robust and expressive language based on relational algebra, for obtaining data from tables . On the other hand, JSON does not use any popular language. In fact, it uses JSON Query Language (JAQL) and JSONiq, which are still evolving query languages.
  • Sorting: In a relational database, SQL easily helps in retrieving data and displaying it in ascending or descending order. In JSON, a developer can sort arrays.
  • Learning Curve: JSON has a much smooth learning curve. This is because the data types and structure supported here are used in several programming languages. Therefore, a developer with basic programming background grasps JSON concepts and coding quite fast. On the other hand, RDBMS is  a distinct field to learn and explore, which takes time to master. However, once mastered, it has its own opportunities and benefits to offer.
  • Application: The market has several commercial as well as open source relational databases to offer. There are also NoSQL databases but they use JSON to store data. JSON is usually implemented in several programming languages.
READ MORE

JSON Support with Programming Languages

Each major programming language can incorporate JSON, which can be through either libraries or native support. However, no language can surpass JavaScript for parsing this data interchange format, which translates it directly into an object literal.

JSON is a popular as XML. Thus, each major language has one or more commonly used powerful libraries for formatting and parsing data in JSON format. Typically, only following two core functionalities are required:

  • Parse: Converts JSON data into the supported data structure of corresponding language, such as array, hash, or dictionary.
  • Format: Converts array, hash, or dictionary to JSON text.

Developers can easily find these functionalities for almost any modern language. For instance, Ruby incorporates JSON gem, Objective-C supports JSONKit, and Microsoft .NET Framework has Json.NET. Most of these libraries are fast and efficient, considering their extensive use and frequent optimizations over time.

While comparing various languages, it might be pointed out by some developers that using JSON in a C# or Java language is not practical. This is because the idioms supports statically typed classes and not objects of Dictionary or HashMap type. As a result, for generating JSON data, developers are required to use a library along with a custom code for converting these data structures to static type instances. Hence, there are some libraries created for this purpose. For instance, the Gson library from Google is designed for transforming JSON data to Java objects directly.

Gson

Gson is an open-source Java library for transforming an object in Java to JSON data and vice-versa. For this purpose, it offers easy means such as constructor (Factory Method) and toString(). This library also functions well with arbitrary Java objects, involving the pre-existing ones whose source code is not available with you. Following are the goals of Gson:

  • Converting already existing not-modifiable objects to and from JSON
  • Permitting custom representations for objects
  • Outputting legible and compact JSON data

Gson is capable of deserializing string of more than 25MB, deserializing 87,000 objects, and serializing of 1.4 million objects without any issues. Its 1.4 version has increased the deserialization bar from 80KB to more than 11MB, applicable for array and collections in bytes.

It is convenient to learn and use Gson. A developer needs to know two methods namely, toJson() and fromJson(). The toJson() method is used for transforming a Java object to JSON data whereas fromJson() for converting JSON data to an object in Java.

READ MORE

Advantages of JSON

                                                           

JSON is primarily used for serializing and de-serializing data being sent to and from an application made using JavaScript. Considering this fact, the advantages of JSON relate to the use of JSON over other option for serialization  and de-serialization.

Typically, in an XML environment, the sender  encodes the data to serialized, by using a DTD that the recipient can comprehend. This task involves a lot of additional padding around the data, regardless of which DTD in use. Thus the size of the XML documents  is significantly  larger than the actual data values to be sent. Then, the recipient obtains the XML data stream and decodes it for putting in the memory.

Comparatively, this process of serialization if done through JSON is quicker and more compact due to the format’s inherent structure. JSON structure uses standard programming data types whose structure is simple to understand. Further, the encoding mechanism is efficient enough to add only minimum characters required to reflect the structure and data values. When the recipient obtains this serialized data, the only procedure required is to evaluate the text through a compatible function in any programming language. For example, evaluation can happen by using eval function of JavaScript.

The other standard to compare is Yaml Ain’t Markup Language (YAML) that serializes  complex data sets without a DTD and relies on a simpler parser than XML. Even such a parser consumes more time than JSON to output larger streams of serialized data.

Following are some more benefits of using JSON:

  • JSON parses faster than XML and YAML.
  • JSON is simpler to work with some languages such as PHP, Python, and JavaScript.
  • JSON is  simpler to map to an object oriented system, as it is data-oriented.
  • JSON is a valid subset of even Python and YAML apart from JavaScript.
  • JSON easily differentiates between the string ‘2’ and the number ‘2’ as well as between a single item and a collection of size one (array).
  • JSON is an ideal data exchange format, and not an ideal document exchange format as XML. Thus, it is not necessary for JSON to be extensible for exchanging documents by defining new tags for representing data.
READ MORE
...