How to Parse Request and Response Messages in JSON Format
How to Parse Request and Response Messages in JSON Format
To set up the REST Web Service Consumer transformation to parse a JSON response, set the log level to Verbose Data in the Developer tool preferences. Open the verbose data log and copy the response to an XML file. Import the XML file as a schema object and use the schema to create the transformation. After you create the transformation, configure the transformation to receive a JSON response.
To set the log level to Verbose Data, complete the following steps:
In the Developer tool, click
Window > Preferences
.
On the left-hand side of the
Preferences
window, click Run Configurations > Mapping.
On the right-hand side, select the
Advanced
tab.
For the property
Override tracing level
, select
Verbose Data
.
Open the verbose data log.
Search for
HTTP Response BODY: <?xml
Copy the data between <xmlRoot> and </xmlRoot> and save the data as an XML file.
Import the XML file as a schema object in the Developer tool.
Step 2. Use the Schema to Create a REST Web Service Consumer Transformation.
To use the schema to parse the data that the REST Web Service Consumer transformation receives, complete the following steps:
In the Developer tool mapping editor, scroll down in the mapping palette to locate the REST Web Service Consumer transformation and drag it into the mapping.
Enter a name for the transformation.
Select an HTTP method.
Click
Next
.
To define the method input, select
Create as empty
or
Create from an element in a Schema Object
. When you define the method input, you define the method input definition and the input mapping.
Click
Next
.
To define the method output, select
Create from an element in a Schema object
Click
Browse
.
Select the element
xmlRoot
.
The following image shows how the element xmlRoot might appear:
Configure the output mapping. If a parent element contains child elements, you must map the child elements manually. Drag the parent and child element groups from the Method output definition area to the Ports area.
You might have a mapping that looks like the following image:
Configure the REST Web Service Consumer transformation properties. For the property
Output Format
, specify
JSON
.
Run the transformation.
The following image shows an example of a response that you might receive:
Note that the data in the response is parsed to form groups that consist of a parent element and nested child elements.