Java API for JSON Processing (JSON-P) - Interview Questions

What is the role of JSON-P API in Java EE?

 FAQ

The Java API for JSON Processing (JSON-P) enables Java EE applications to parse, transform, and query JSON data using either the Object model or the Streaming model.

JSON is a text-based data exchange format derived from JavaScript that is used in Web Services and other connected applications.

JSON-P is introduced in the Java EE 7 platform.

What are the two programming models used for generating and parsing JSON data?

 FAQ

There are two programming models used for generating and parsing JSON data. Object model and Streaming model.

The Object model creates a tree structure in memory that represents the JSON data. The tree can then be navigated, analyzed, or modified.

The Streaming model uses an event-based parser that reads JSON data one element at a time. Each element can be processed or discarded by the application code, and then the parser proceeds to the next event.

The parser generates events and stops for processing when an object or an array begins or ends, when it finds a key, or when it finds a value.

What are the key packages provided in the JSON-P API?

 FAQ

The JSON-P API provides the following key packages to generate and parse JSON data.

javax.json - This package contains interfaces and classes for generation and parsing JSON data using Object model.

javax.json.stream - This package contains interfaces and classes for generating and parsing JSON data using Streaming model.

What are the key classes and interfaces provided in javax.json package?

 FAQ

javax.json package contains the following key classes and interfaces to generate and parse JSON objects using the object model.

Json - Contains static methods to create instances of JSON parsers, builders and generators.

JsonReader - Reads JSON data as stream and create an object model in memory.

JsonObjectBuilder - Creates object model in memory by adding elements through application code.

JsonWriter - Writes an object model from memory to a stream.

What are the key classes and interfaces provided in javax.json.stream package?

 FAQ

javax.json.stream package contains the following key classes and interfaces to generate and parse JSON objects using the stream model.

JsonParser - Event based parser that can read JSON data from a stream.

JsonGenerator - Generates JSON data to a stream, one element at a time.

How do you create a JSON object model from JSON data using JSON-P API?

 FAQ

You can create an object model using JSON data using following steps

1. Create an instance of JsonReader class from the Json utility class and pass the JSON data file as a parameter.

2. Call read() method on the reader instance which returns the JSON object model.

JsonReader reader = Json.createReader(new FileReader('jsondata.txt'));
JsonStructure jsons = reader.read();

How do you create a JSON object model from application code using JSON-P API?

 FAQ

You can create a JSON object model using application code using following steps

1. Create an instance of JsonObject class using the Json utility class.

Add key value pairs, objects or arrays to the instance of JsonObject.

JsonObject modelObject = Json.createObjectBuilder();
modelObject.add('firstName', 'Java');
modelObject.add('lastName', 'Guru');
modelObject.add('city', 'San Francisco');
modelObject.build();

How do you write a JSON object model to a stream using JSON-P API?

 FAQ

You can output a JSON object model to a stream by following steps.

1. Create an instance of JsonWriter by using the Json utility class.

2. Call the method writeObject() on the instance of JsonWriter and pass the instance of JSON model as a parameter.

StringWriter writer = new StringWriter();
JsonWriter jsonWriter = Json.createWriter(writer);
jsonWriter.writeObject(model);
jsonWriter.close();

//print the json data
String jsonData = stWriter.toString();
System.out.println(jsonData);
 
Subscribe to our Questions

 

Java EE - Interview Questions

Java ServletsJavaServer PagesJavaServer FacesJava API for XML Processing (JAXP)Java API for JSON Processing (JSON-P)Java Message Service API (JMS)Java Persistance API (JPA)Java Transaction API (JTA)JAX-WSJava API for RESTful Web Services (JAX-RS)Java Database Connectivity API (JDBC)Security
 
RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example