{ Community }
  • Academy
  • Docs
  • Developers
  • Resources
    • Community Articles
    • Apigee on GitHub
    • Code Samples
    • Videos & eBooks
    • Accelerator Methodology
  • Support
  • Ask a Question
  • Spaces
    • Product Announcements
    • General
    • Edge/API Management
    • Developer Portal (Drupal-based)
    • Developer Portal (Integrated)
    • API Design
    • APIM on Istio
    • Extensions
    • Business of APIs
    • Academy/Certification
    • Analytics
    • Events
    • Hybrid
    • Integration (AWS, PCF, Etc.)
    • Microgateway
    • Monetization
    • Private Cloud Deployment
    • Insights
    • IoT Apigee Link
    • BaaS/Usergrid
    • BaaS Transition/Migration
    • Apigee-127
    • New Customers
    • Topics
    • Questions
    • Articles
    • Ideas
    • Leaderboard
    • Badges
  • Log in
  • Sign up

Get answers, ideas, and support from the Apigee Community

  • Home /
  • Edge/API Management /
avatar image
0
Question by Sujith Mathew · Aug 28, 2017 at 10:08 AM · 1.1k Views javascriptassign message policyjsontoxml

Conversion of Query Parameters to XML

hi,

we are trying to convert json input parameters to XML

ex

query paramerer?name=abc1&name=abc2&name=abc3

to

<name>abc1</name><name>abc2</name><name>abc3</name>

the number of "name" values is dynamic.

I'm able to retrieve the query parameters of "name" using JavaScript .

But facing difficulty to set the values as XML payload using assign message policy.

Is there a methodology to convert to XML payload in this scenario?

Comment
Add comment
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Close

1 Answer

  • Sort: 
avatar image
1
Best Answer

Answer by Dino   · Aug 28, 2017 at 05:06 PM

Hi -

I'm not clear on what you want to do. You are saying "json input parameters" but I don't see any JSON. JSON is defined on json.org ; it is a data format, like XML, that allows you to encode arbitrary data. A simple example of a JSON payload representing an address might be:

{
  "address" : {
    "line1" : "123 Main Street",
    "line2" : "Apartment 345",
    "city" : "Belltown",
    "state" : "MD",
    "country" : "US",
    "postalCode" : 12345
  }
}  

JSON uses curly braces, double quotes, colons, and commas to format the data. In your example, you showed none of that. No curlies, no quotes, no commas. So I don't think you are using JSON.

It seems to me that this example:

query paramerer?name=abc1&name=abc2&name=abc3

...represents a query string. Not the same as JSON.

I'm sorry about being pedantic, but now let's get to your question.

I think what you want to do is transform the above querystring data into an XML format. And you say the number of "name" values is dynamic - sometimes there are 2, sometimes 3 or more.

In general, you cannot produce an XML equivalent with AssignMessage. The reason is because the number of occurrences of "name" is dynamic. If the input querystring always had 2 occurrences, or always had 3 occurrences, then you would be able to use an AssignMessage. But you don't have that situation. You have a dynamic number of occurrences of the 'name' parameter. To handle a dynamic number of occurrences, you need a loop, and AssignMessage does not have an internal loop construct. But there is a way to do what you want!

The best way to do what you want may be to loop through the query parameters with logic in a JavaScript callout. For example, the JS code might be like this:

var xmlOutput = '';
var queryStringPairs = context.getVariable("request.querystring").split("&");
queryStringPairs.forEach(function(nameValuePair) {
  var nameAndValue = nameValuePair.split('=');
  if (nameAndValue.length == 2) { // is it a name=value form?
    if (nameAndValue[0] == 'name') {
      xmlOutput += '  <name>' + nameAndValue[1] + '</name>\n';
    }
  }
});
xmlOutput = '<root>\n' + xmlOutput + '</root>\n';
context.setVariable('equivalent_xml', xmlOutput);

What is happening there?

  • get the inbound query string, and split it by ampersands
  • for each of those query parameters, split THAT on the equals sign
  • only if the name part is 'name', append an element to an XML string
  • wrap the XML string in a root element
  • set a context variable with the resulting XML-formatted string.

And the result can be like this:

$ curl -i 'https://ORG-ENV.apigee.net/sujith-qparams/t1?name=abc1&name=abc2&name=abc3'
HTTP/1.1 200 OK
Date: Mon, 28 Aug 2017 16:59:48 GMT
Content-Type: application/xml
Content-Length: 75
Connection: keep-alive
Server: Apigee Router


<root>
  <name>abc1</name>
  <name>abc2</name>
  <name>abc3</name>
</root>

See the attached proxy. sujith-qparams.zip


sujith-qparams.zip (4.6 kB)
Comment
Add comment Show 1 · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sujith Mathew · Sep 01, 2017 at 11:09 AM 0
Link

Excellent illustration , Dino , tried it out and working fine in our "real life" issue

Thank you.

Follow this Question

Answers Answers and Comments

57 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Extent the time to get the response from the target to make service callout request. 1 Answer

​Condition not working in Assign Message Policy. Is any possibility to include conditional logic in an AssignMessage policy? 2 Answers

How can i convert Xml data to Json and Json to Xml using Extract Variable or Assign Message Policy? 1 Answer

How to Append dynamic request path to target end point based on request method. 1 Answer

Json to XML Array within Array with Different Parent Names 2 Answers

  • Products
    • Edge - APIs
    • Insights - Big Data
    • Plans
  • Developers
    • Overview
    • Documentation
  • Resources
    • Overview
    • Blog
    • Apigee Institute
    • Academy
    • Documentation
  • Company
    • Overview
    • Press
    • Customers
    • Partners
    • Team
    • Events
    • Careers
    • Contact Us
  • Support
    • Support Overview
    • Documentation
    • Status
    • Edge Support Portal
    • Privacy Policy
    • Terms & Conditions
© 2019 Apigee Corp. All rights reserved. - Apigee Community Terms of Use - Powered by AnswerHub
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Create an article
  • Post an idea
  • Spaces
  • Product Announcements
  • General
  • Edge/API Management
  • Developer Portal (Drupal-based)
  • Developer Portal (Integrated)
  • API Design
  • APIM on Istio
  • Extensions
  • Business of APIs
  • Academy/Certification
  • Analytics
  • Events
  • Hybrid
  • Integration (AWS, PCF, Etc.)
  • Microgateway
  • Monetization
  • Private Cloud Deployment
  • Insights
  • IoT Apigee Link
  • BaaS/Usergrid
  • BaaS Transition/Migration
  • Apigee-127
  • New Customers
  • Explore
  • Topics
  • Questions
  • Articles
  • Ideas
  • Members
  • Badges