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 691
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

@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

Not applicable

Hi @sudheendra1, thanks for the solution but I'm getting 401 error

 HTTP/1.1 401 UnauthorizedDate:Thu, 25 Jun 2015 04:37:47 GMTAccess-Control-Allow-Origin:*Content-Length:213Set-Cookie:rememberMe=deleteMe; Path=/; Max-Age=0; Expires=Wed, 24-Jun-2015 04:37:47 GMTConnection:keep-aliveContent-Type:application/jsonServer:Apache-Coyote/1.1
{
  "error": "service_resource_not_found",
  "timestamp": 1435207067210,
  "duration": 0,
  "exception": "org.apache.usergrid.services.exceptions.ServiceResourceNotFoundException",
  "error_description": "Service resource not found"
}

Can you check my proxy and tell me why?

What's the endpoint you are trying to access? I have imported your proxy in to my org. I don't see any error. Here is the link -

http://sudheendra-test.apigee.net/roles 

Response -

{
  "action" : "get",
  "application" : "76571250-f8be-11e4-b256-4756d201fdeb",
  "params" : { },
  "path" : "/roles",
  "uri" : "https://api.usergrid.com/onlinedinesh/sandbox/roles",
  "entities" : [ {
    "uuid" : "70f39f36-1825-379d-8385-7a7fbe9ec74a",
    "type" : "role",
    "name" : "admin",
    "created" : 1431445764101,
    "modified" : 1431445764101,
    "roleName" : "admin",
    "title" : "Administrator",
    "inactivity" : 0,
    "metadata" : {
      "path" : "/roles/70f39f36-1825-379d-8385-7a7fbe9ec74a",
      "sets" : {
        "permissions" : "/roles/70f39f36-1825-379d-8385-7a7fbe9ec74a/permissions"
      },
      "collections" : {
        "groups" : "/roles/70f39f36-1825-379d-8385-7a7fbe9ec74a/groups",
        "users" : "/roles/70f39f36-1825-379d-8385-7a7fbe9ec74a/users"
      }
    }
  }, {
    "uuid" : "b8f8f336-30c9-3553-b447-6891f3e1e6bf",
    "type" : "role",
    "name" : "default",
    "created" : 1431445764119,
    "modified" : 1431445764119,
....

When I give /xml  I get this error


http://org-test.apigee.net/roles/xml          gives, 

 HTTP/1.1 401 UnauthorizedDate:Thu, 25 Jun 2015 04:37:47 GMTAccess-Control-Allow-Origin:*Content-Length:213Set-Cookie:rememberMe=deleteMe; Path=/; Max-Age=0; Expires=Wed, 24-Jun-2015 04:37:47 GMTConnection:keep-aliveContent-Type:application/jsonServer:Apache-Coyote/1.1
{
  "error": "service_resource_not_found",
  "timestamp": 1435207067210,
  "duration": 0,
  "exception": "org.apache.usergrid.services.exceptions.ServiceResourceNotFoundException",
  "error_description": "Service resource not found"
}

@Anil Sagar @sudheendra1