Tom Donohue Tom Donohue

Returning JSON from a REST service in Talend

If you’re creating web services that are going to be accessed from a JavaScript application, chances are you’re going to want to return JSON insead of XML in your response.

XML is cool (or is it just me who thinks that?) but JSON is nicer.

JSON is light, flexible and plays well with JavaScript, especially with frameworks like jQuery.

But if you’re reading this article you’re probably already convinced. You’ve decided you want to implement a REST service in a Talend Job and want to return JSON in the response.

The good news is that you don’t really need to do much. Support for JSON is built in to the tRESTRequest and tRESTResponse components - as long as you use them in the right way.

Firstly you need to tell tRESTRequest to produce data in “XML or JSON” when you configure the component.

Then you do whatever you need to generate the response. Fetch a record from the database, read something from a file, whatever.

Finally, when you’re ready to return the response, make sure you pass a Document to tRESTResponse. Talend will do the rest, returning either XML or JSON to the consumer, depending on what the consumer likes.

By default, the service will return XML. But the consumer can specify the response format using content negotiation. This is done by sending an HTTP Accept: header along with the request, to say that the response should be sent as JSON.

The example below illustrates this, and shows how to test the service using SoapUI.

Creating a REST service that returns JSON

In this example I’m going to create a simple REST service with one HTTP GET operation. It will return some information about a customer in either XML or JSON, depending on what the consumer asks for.

  1. Create a new job in Talend Studio.

  2. Drag three components from the Palette onto the canvas - tRESTRequest, tXMLMap and tRESTResponse. Wire them up as shown:

    tRESTRequest, tXMLMap and tRESTResponse components

  3. Double-click the tRESTRequest component to show its properties. Ensure that the REST API Mapping table looks like the screenshot below. Specifically, the “Produces” column should show “XML or JSON”:

    tRESTRequest component properties

  4. In the tXMLMap, define your response payload. In this example, I’ve created a payload containing some information about a Customer. I’ve defined a nested structure here - with customer at the top, and with an address element containing some child elements.

    tXMLMap output

  5. Run the Job.

“Is that it?” you might be asking.

Yes. This service is now ready to return JSON or XML.

But we need to test it. So let’s do that.

Testing the response

To test the response I use soapUI. We’ll set up a project and send a test request to your service. Follow these steps:

  1. Ensure your Job is running in Studio, if it isn’t already.

  2. In SoapUI, click File → New REST Project.

  3. Enter the URL http://localhost:8088 (NB: This is the default if you’re using Talend 5.6; if you’re running a different version, or you’ve customised the URL, you can take a look at the tRESTRequest component settings in Studio to see the URL of your REST service).

  4. Once SoapUI has set up the new REST project, find the test request window (Request 1) and click the green Play button to send a request to the service. The HTTP method should be GET, as shown. See that by default, the service returns XML.

    soapUI request

  5. Now let’s get the service to return JSON. In the Request window, click on the Headers button at the bottom left to show the Headers pane.

  6. Add a custom HTTP header by clicking the small icon (marked with a plus sign). Set the header name to Accept and set its value to application/json, as shown:

    soapUI accept JSON

  7. Now rerun the test by clicking the green Play button. See how the same customer is returned, but this time as JSON:

    soapUI response in JSON

Magic.


Key points

  • Talend’s tRESTRequest and tRESTResponse components can return response as either XML or JSON, as long as the response is generated from a Document
  • The response type depends on what the consumer asks for
  • To receive a JSON response, the consumer should send an Accept HTTP request header with a value of application/json

Comments

What do you think? You can use Markdown in your comment.

To write code, indent each line with 4 spaces. Or, to paste a lot of code, you can put it in pastebin.com and share the link in your comment.