Java APIs for JSON Processing

 

Java APIs for JSON Processing


In the previous chapter, you were introduced to the REST architectural style. Remember that REST does not prescribe any specific message format for client-server communication. One can use an appropriate format for representing messages as long as the chosen format is supported by HTTP. XML and JSON are the two popular formats used by RESTful web services today. Within these two formats, JSON is widely adopted by many vendors because of its simplicity and light weight. In this chapter, you will learn more about the JSON message format, how to represent real-life business data in the JSON format, and various processing tools and frameworks related to JSON. 

The following topics are covered in this chapter:

  • A brief overview of JSON
  • Using the JSR 353 – Java API for processing JSON
  • Using the Jackson API for processing JSON
  • Using the Gson API for processing JSON
  • Java EE 8 Enhancements for processing JSON

A brief overview of JSON

JSON is a lightweight, text-based, platform-neutral data interchange format in which objects are represented in the attribute-value pair format. Historically, JSON originated from JavaScript. However, nowadays, it is considered to be a language-independent format because of its wide adoption by all modern programming languages. In this section, you will learn the basics of the JSON format.

Understanding the JSON data syntax

The JSON format is very simple by design. It is represented by the following two data structures:

  • An unordered collection of the name-value pairs (representing an object):
    • Attributes of an object and their values are represented in the name-value pair format; the name and the value in a pair are separated by a colon (:). Names in an object are strings, and values may be of any of the valid JSON data types such as number, string, Boolean, array, object, or null. Each name:value pair in a JSON object is separated by a comma (,). The entire object is enclosed in curly braces ({}).
    • For instance, the JSON representation of a department object is as follows:
{"departmentId":10, "departmentName":"IT",  
"manager":"John Chen"}
    • This example shows how you can represent the various attributes of a department, such as departmentIddepartmentName, and manager in the JSON format.
  • An ordered collection of values (representing an array):
    • Arrays are enclosed in square brackets ([]), and their values are separated by a comma (,). Each value in an array may be of a different type, including another array or an object.
    • The following example illustrates the use of array notation to represent the employees working in a department. You can also see an array of locations in this example:
{"departmentName":"IT",  
"employees":[
{"firstName":"John", "lastName":"Chen"},
{"firstName":"Ameya", "lastName":"Job"},
{"firstName":"Pat", "lastName":"Fay"}
],
"location":["New York", "New Delhi"]
}

 

Basic data types available with JSON

While discussing the JSON syntax in the previous section, we glanced at some of the basic data types used in JSON. Let's take a detailed look at each item.

Here is the list of the basic data types available with JSON:

  • Number: This type is used for storing a signed decimal number that may optionally contain a fractional part. Both integer and floating point numbers are represented by using this data type. The following example uses the decimal data type for storing totalWeight:
{"totalWeight": 123.456} 
  • String: This type represents a sequence of zero or more characters. Strings are surrounded with double quotation marks and support a backslash escaping syntax. Here is an example for the string data type:
{"firstName": "Jobinesh"} 
  • Boolean: This type represents either a true or a false value. The Boolean type is used for representing whether a condition is true or false, or to represent two states of a variable (true or false) in the code. Here is an example representing a Boolean value:
{"isValidEntry": true} 
  • Array: This type represents an ordered list of zero or more values, each of which can be of any type. In this representation, comma-separated values are enclosed in square brackets. The following example represents an array of fruits:
{"fruits": ["apple", "banana", "orange"]} 
  • Object: This type is an unordered collection of comma-separated attribute-value pairs enclosed in curly braces. All attributes must be strings and should be distinct from each other within that object. The following example illustrates an object representation in JSON:
{"departmentId":10, 
"departmentName":"IT",
"manager":"John Chen"}
  • null: This type indicates an empty value, represented by using the word null. The following example uses null as the value for the error attribute of an object:
{"error":null} 

The example in the next section illustrates how you can use the JSON data types that we discussed in this section to represent the details of employees.


Sample JSON file representing employee objects

Here is a sample JSON document file called emp-array.json, which contains the JSON array of the employee objects. The content of the file is as follows:

[ {"employeeId":100,"firstName":"John","lastName":"Chen", 
"email":"john.chen@xxxx.com","hireDate":"2008-10-16"},
{"employeeId":101,"firstName":"Ameya","lastName":"Job",
"email":"ameya.job@xxx.com","hireDate":"2013-03-06"},
{"employeeId":102,"firstName":"Pat","lastName":"Fay",
"email":"pat.fey@xxx.com","hireDate":"2001-03-06"} ]
javax.json.JsonValue does not have a DATE ValueType. In the previous example, hireDate will be treated as a STRING type, so handling of DATE values requires explicit parsing as per the date format.

We will use this emp-array.json file as the input source for many examples that we will discuss later in this chapter. By now, you should have a fairly good understanding of the JSON syntax. We will later learn the various techniques for processing JSON data.

Comments

Popular posts from this blog

Understanding the JAX-RS resource life cycle

Generating a chunked output using Jersey APIs