From the course: JSON Processing with Java EE

Work with JSON and Java

From the course: JSON Processing with Java EE

Start my 1-month free trial

Work with JSON and Java

- [Instructor] Let's have a look at some of the common usages of JSON in the Java world with a focus on its use in the Java enterprise edition ecosystem. As you are already familiar with JSON, I will dispense with any discussion of its structure. What I want to do is to ensure that you are familiar with the way JSON and Java work together. So let's start by looking at a simple structure like this. This is a JSON object with a property called title, which has an associated text value JSON Processing with Java EE. If this JSON string was represented as a Java class, it would have a structure like so. As you can see, the JSON property title is represented as a field with the same name. An instantiation of the course class would have the title field value set to the same value as that which you see in the JSON string. A more complex example might include a JSON array like this. This array will be represented as a list typing the Java class as you can see here on the screen; and in a slightly more complex example, we could have a nested JSON object such as the course code JSON object you see here. This JSON string will be represented as a class that has a title, chapters, and course code, has members, and the source code object will be represented by the source code class. This is the extent of the relationship between the JSON structure and a JSON class. The variable type might be different to those in the example, but will be restricted to a string, a numerical type, an array, or an object. So as you can see, Java and JSON fit together very nicely. There are no hacks or awkward relationships to force cooperation. In fact, Java and JSON are really very well suited for each other; and this is no coincidence. JSON derives from JavaScript, which has a very similar syntax to Java. So let's have a look at some of the ways that we use JSON and Java in harmony. Arguably, one of the most common usages of JSON is in the interchange of data between a client and a RESTful API. A RESTful API implements the REST architectural pattern, where REST means representational state transfer. So, what does this mean? Well, without going into lots of detail, often what this boils down to is the civilization of a plain old Java object represented as a JSON string. We've seen an example of this in the previous slides. The POJO represents a resource within your system, such as a book in the bookshop application or a course in an online training site. Although it is not required that JSON be used to represent resources, it has nevertheless become the predominantly used technology; and it is within REST that Java and JSON really can be seen to work in unison. Connected with the REST paradigm is the use of JSON web tokens, JWT, to implement security concerns. JWT is an open source specification that can be used across REST-compliant systems and is not just restricted to use in Java applications, but any application that understands JSON. A JSON web token is used to create axis tokens that assert a claim, usually in relation to authorization; but often they are used to pass the identity of authenticated users between systems. If you would like to know more, then go to the jwt.io website where you will find a lot more information. The next generation of databases are referred to as NoSQL and document stores. They store data in a non-normalized manner in documents structured as JSON strings. So instead of storing data in tables, rows, and columns; the data is stored as JSON documents and often with different shapes. A JSON document is just another way of saying JSON string or JSON data. The terms can be used interchangeably. The rise of this type of data store has increased the importance of working efficiently with JSON in a Java application context. Luckily, as Java developers, we are well catered for as far as compatible and extensive JSON processing APIs go. Why not have a look at some of the NoSQL data stores listed here. By no means is this an exhaustive list; there are plenty more out there, but you can get started with these. The usage of JSON is growing and having a solid knowledge of the Java enterprise APIs that can be used to process JSON documents is absolutely vital. In this course, you will learn how to use the most important features of the JSON processing API to the point where you will be able to immediately put into action what you have learned.

Contents