Unable to convert JsonToXml and XmlToJson data.

Hi Team,

I have a usecase where I have to convert my request data JsonToXml and XmlToJson.

My First TargetEndPoint is : (JsonEndPoint)

http://www.mocky.io/v2/5cb592b9330000f5175d7bfe

Response for this First Target is : application/json

{ "Details" : { name: "Rishu", age: 22, city: "India" }}

My Second TargetEndPoint is : (XmlEndPoint)

http://www.mocky.io/v2/5cbe9fa7300000f2069ce1b7

Response for this First Target is : application/xml

  <Books>
  <C>Er. Smitha Swetha</C>
  <Java>Dr. Ranjan Singh</Java>
  <Paython>Mr. Abdul Kaadir</Paython>
  </Books>

Now I'm sending 1st Post Request from Postman :

{
"target" : "json",
"response" : "Json"
}

// Getting Correct Response which is My TargetEndPoint-1(JsonEndPoint) data.

Now I'm sending 2nd Post Request from Postman :

{
  "target" : "Xml",
  "response" : "Xml"
}

// Getting Correct Response which is My TargetEndPoint-2(XmlEndPoint)data.

Now when I'm sending 3rd Post Request from Postman :

{
  "target" : "json",
  "response" : "Xml"
}

Getting an Error:

{
    "fault": {
        "faultstring": "Execution of JsonToXmlJs failed with error: Javascript runtime error: "TypeError: Cannot read property "response" from undefined. (JsonToXmlJs.js:5)"",
        "detail": {
            "errorcode": "steps.javascript.ScriptExecutionFailed"
        }
    }
}

My Expected Output is below : (It Must hit the TargetEndpoint-1(JsonEndPoint) and convert it as Xml and give response back )

  <Details>
  <age>22</age>
  <city>India</city>
  <name>Rishu</name>
  </Details>

Now when I'm sending 4th Post Request from Postman :

{
  "target" : "Xml",
  "response" : "Json"
}

Getting Error

{
    "fault": {
        "faultstring": "Execution of JsonToXmlJs failed with error: Javascript runtime error: "TypeError: Cannot read property "response" from undefined. (JsonToXmlJs.js:5)"",
        "detail": {
            "errorcode": "steps.javascript.ScriptExecutionFailed"
        }
    }
}

My Expected Output is below : (It Must hit the TargetEndpoint-2(XmlEndPoint) and convert it as Json and give response back )

{
   "Books": {
     "C": "Er. Smitha Swetha",
     "Java": "Dr. Ranjan Singh",
     "Paython": "Mr. Abdul Kaadir"
   }
}


First JavaScript code is : In ProxyEndPoint Preflow Request :

var body = request.content.asJSON;
context.setVariable("target", body.target);
var v = context.getVariable('target');
print(v);

Second JavaScript code is : In ProxyEndPoint Preflow Request :

var body = request.content.asXML;
context.setVariable("target", body.target);
var v = context.getVariable('target');
print(v);

Third JavaScript code is : In ProxyEndPoint Preflow Response :

var body = response.content.asJSON;
context.setVariable("response", body.response);
var v1 = context.getVariable('response');
print(v1);

Fourth JavaScript code is : In ProxyEndPoint Preflow Response :

var body = response.content.asXML;
context.setVariable("response", body.response);
var v1 = context.getVariable('response');
print(v1);

RouteRule Condition Is :

  <RouteRule name="MyRoute">
  <Condition>target = "json"</Condition>
  <TargetEndpoint>JsonEndPoint</TargetEndpoint>
  </RouteRule>
    <RouteRule name="MyRouteNew">
  <Condition>target = "xml"</Condition>
  <TargetEndpoint>XmlEndPoint</TargetEndpoint>
  </RouteRule>


I Used JsonToXml Policy In TargetEndPoint-1(JsonEndPoint) Preflow Response with Condition :

<Response>
  <Step>
    <Name>JsonToXml</Name>
    <Condition>(response.header.Content-type = "application/json") and (response = "json")</Condition>
  </Step>
</Response>

And XmlToJson Policy In TargetEndPoint-2(XmlEndPoint) Preflow Response with Condition :

     <Response>
  <Step>
  <Name>JsonToXml</Name>
  <Condition>(response.header.Content-type = "application/xml") and (response = "xml")</Condition>
  </Step>
  </Response>

Condition for all my javascript in ProxyEndPoint :

  <PreFlow name="PreFlow">
  <Request>
  <Step>
  <Name>JsonJs</Name>
  <Condition>(request.header.Content-type = "application/json")</Condition>
  </Step>
  <Step>
  <Name>XmlJs</Name>
  <Condition>(request.header.Content-type = "application/xml")</Condition>
  </Step>
  </Request>
  <Response>
  <Step>
  <Name>JsonToXmlJs</Name>
  <Condition>(response.header.Content-type = "application/json")</Condition>
  </Step>
  <Step>
  <Name>XmlToJsonJs</Name>
  <Condition>(response.header.Content-type = "application/xml")</Condition>
  </Step>
  </Response>
  </PreFlow

Any Advice what's wrong here?
Could Someone please help me out?
Is I need to add any other policy for this work?

@Anil Sagar @ Google,@Dino-at-Google,@Brendan,@deboraelkin,@Siddharth Barahalikar,@Priyadarshi Ajitav Jena,@Robert Johnson,@AnilSagar,@sudheendras,@Mukundha Madhavan,@Jeremy Whitlock,@Nagashree B

Please let me know how to resolve this issue.

Thanks in Advance.
Rishika

Solved Solved
0 13 365
1 ACCEPTED SOLUTION

Hi @Rishika Kumari, I found your proxy too complicated for a simple XML>>JSON>>XML conversion. Why use that many Javscripts policies? We should use JS policy only when other policies are not solving our usecase.

Moreover why are you using a POST method for format conversion? Use HTTP Header for passing request values to the proxy.

I have modified the proxy, removed all JS policies. Used request headers to pass target & response values. Check the policies and conditions used. Post here for any queries.

Make calls using GET method and headers in Postman.

PFA 8542-sunapend-rev3-2019-05-05-rev2-2019-05-06.zip

8549-aapigee.png

View solution in original post

13 REPLIES 13

@Rishika Kumari - The json mock response is not a valid one. Change it to

{ "Details" : { "name": "Rishu", "age": 22, "city": "India" }}

and see if it works

Thank you for your time @Sai Saran Vaidyanathan

But there is no problem in Mocky data, Because It's working when my "target" and "response" both as json.


I tried to detail & explain my problem as much as possible, if you have any question or tip please feel to comment.

Could Someone please advice me what's wrong here?

As mentioned by Sai, the JSON response you are getting from http://www.mocky.io/v2/5cb592b9330000f5175d7bfe is invalid.

You can use any JSON Validator (https://jsonlint.com/) to check the JSON.

Cannot read property \"response\" from undefined

It cannot read the response because it is getting an invalid JSON structure.

@Siddharth Barahalikar

Thank you for your time&work,
Maxime

I changed my response as said by Sai and validate that too.

But unfortunately, I'm getting all the errors as before.

Can you attach the sample proxy used.

1. Don't do this:

context.setVariable("response", body.response);

The "response" context variable in apigee edge is a well known variable, and should not be set to an arbitrary value. You can read more about the variable in the documentation.

2. I think the error that you're getting....

Cannot read property "response" from undefined

....is probably caused by that line. Specifically the body.response reference. body is undefined.

Doing this:

var body = response.content.asXML;

Gives you an XML object. Look up e4x. body is an XML.

body.response is probably not defined. What are the elements in your XML? You'll have to debug it.

Likewise

var body = response.content.asJSON;

...gives you a JSON hash. body.response is not defined. What are the properties in the JSON? We cannot tell you. You have to figure this out; it's whatever your upstream has returned.


In summary

  1. The errors you are seeing are runtime errors in your JavaScript code. You need to debug them.
  2. You could avoid all of this by using the builtin policies. For a reason you have not shared with us, you don't want to use the builtin policies.

@Dino-at-Google

Thanks you for support!!

Hi @Rishika Kumari, I found your proxy too complicated for a simple XML>>JSON>>XML conversion. Why use that many Javscripts policies? We should use JS policy only when other policies are not solving our usecase.

Moreover why are you using a POST method for format conversion? Use HTTP Header for passing request values to the proxy.

I have modified the proxy, removed all JS policies. Used request headers to pass target & response values. Check the policies and conditions used. Post here for any queries.

Make calls using GET method and headers in Postman.

PFA 8542-sunapend-rev3-2019-05-05-rev2-2019-05-06.zip

8549-aapigee.png

@Siddharth Barahalikar

Thanks, But my requirement is for post method as I mentioned above. And I used four javascripts two for request (one for json and one for xml) and two for response(one for json and one for xml)

.

But my requirement is for post method as I mentioned above.

!! Cmon. You can easily change Siddharth's example to use POST instead of GET.

And I used four javascripts two for request (one for json and one for xml) and two for response(

Siddharth explained how to simplify your API Proxy. I suggest that you consider his advice more carefully before dismissing it!