One common Apigee interview question is: What is the JSONToXML policy in Apigee, and when would you use it?
The JSONToXML policy converts a JSON-formatted request or response payload into XML. It is useful when an API client sends JSON but the backend system expects XML, such as a legacy SOAP service.
1. How Does JSONToXML Work?
For example, a client sends a JSON request to Apigee, but the backend expects XML.
JSON input:
{
"name": "John",
"amount": 1000
}
XML output:
<Root>
<name>John</name>
<amount>1000</amount>
</Root>
Apigee transforms the payload before forwarding it to the backend.
2. JSONToXML Policy XML Example
<JSONToXML name="JSON-To-XML">
<Source>request</Source>
<OutputVariable>request</OutputVariable>
<Options>
<RootElement>Root</RootElement>
</Options>
</JSONToXML>
Explanation:
-
Source: Specifies the variable containing the JSON payload. -
OutputVariable: Specifies where the converted XML is stored. -
RootElement: Defines the root element of the generated XML document.
In this example, the policy reads the request payload and writes the converted XML back to the request variable.
3. When Would You Use JSONToXML?
Use the JSONToXML policy when:
- A client sends JSON, but the backend expects XML.
- You need to integrate modern REST APIs with legacy XML-based systems.
- You need to transform message formats within an Apigee API proxy.
4. Where Should You Place the Policy?
If the backend expects XML, attach the JSONToXML policy to the ProxyEndpoint request flow, before the request is routed to the backend.
Ensure that the request's Content-Type header is updated to application/xml when required by the backend.
5. JSONToXML vs. XMLToJSON
| Feature | JSONToXML | XMLToJSON |
|---|---|---|
| Purpose | Converts JSON to XML | Converts XML to JSON |
| Typical use | JSON client to XML backend | XML backend to JSON client |
| Input | JSON payload | XML payload |
| Output | XML payload | JSON payload |
6. Frequently Asked Interview Questions
Q1. Does JSONToXML convert the entire HTTP request?
No. It transforms the message payload. Headers and other request properties must be handled separately when necessary.
Q2. Can JSONToXML be used for response transformation?
Yes. You can use it wherever the source variable contains JSON and the converted XML is required, including response processing.
Q3. Does JSONToXML automatically update the Content-Type header?
Do not assume that it does. Set the appropriate Content-Type header explicitly when required.
Q4. What is the difference between JSONToXML and XSLTransform?
JSONToXML converts JSON into XML. XSLTransform applies an XSL stylesheet to transform an XML document into another XML structure or format supported by the stylesheet.
Conclusion
The JSONToXML policy in Apigee converts JSON payloads into XML, enabling integration between JSON-based APIs and XML-based backend systems.
Interview tip: Remember the three key concepts: Source, OutputVariable, and RootElement. Also, know where to attach the policy and when to update the Content-Type header.
Official documentation: https://docs.cloud.google.com/apigee/docs/api-platform/reference/policies/json-xml-policy
Top comments (0)