Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Converting between XML and JSON

Not applicable

Hi @Anil Sagar @Maruti Chand @Mike Dunker @sudheendra1, I'm trying to get something like this,

https://maps.googleapis.com/maps/api/geocode/json?address=CA%2094043

https://maps.googleapis.com/maps/api/geocode/xml?address=CA%2094043

So if we give /json or /xml the response should be in that format.

How can I get this through an edge proxy?

My proxy reponse is in JSON. So if I add /xml I should get response in XML.

Is this covered in xml-json/json-xml policies?(If yes, then I might have missed it)

I have seen this, but I could understand it completely,

https://community.apigee.com/articles/1839/converting-between-xml-and-json-what-you-need-to-k.html

Solved Solved
0 4 771
1 ACCEPTED SOLUTION

@online dinesh You need to have the JSON-XML policy execute on a condition. In the following example "JSON-to-XML-1" policy is executed only if the path suffix is "/xml"

    <PreFlow name="PreFlow">
        <Request/>
        <Response>
            <Step>
                <Condition>proxy.pathsuffix == "/xml"</Condition>
                <Name>JSON-to-XML-1</Name>
            </Step>
        </Response>
    </PreFlow>

When you add this condition, policy gets executed only for https://maps.googleapis.com/maps/api/geocode/xml?address=CA%2094043.

But I won't encourage you to follow this URL convention. I think the most sensible design option here would be to append a .json or .xml extension to the endpoint. Eg -

https://maps.googleapis.com/maps/api/geocode.json?address=CA%2094043

https://maps.googleapis.com/maps/api/geocode.xml?address=CA%2094043

Alternatively you can specify the media type in "Accept" header or as a query parameter (format=json) in your request

View solution in original post

4 REPLIES 4
Top Solution Authors