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

In Edge, is it possible to Run a Policy in a Loop?

Not applicable

Hi,

I want to be able to call an apigee policy in a javasccript loop (for loop to be exact).

Specifically, I have an array of json objects. I want to run the JSON to XML policy on each json object in the array, and get back an array of xml (strings technically, but in xml structure).

I could give the JSON to XML policy the entire array, but then I would have to put that into another script to take the massive string and turn it back into an array. I would prefer to not do that.

Thank you,

Aleks


Addendum

I have this (simplified).

json =

{
 "array": [
   {"a":"1"},
   {"b":"2"},
   {"c":"3"}
  ]
}

I want to convert it to (again simplified).

json =

{
  "array": [
    "<a>1</a>",
    "<b>2</b>",
    "<c>3</c>"
  ]
}

In the real case each json in the array is much longer, and I do not know how many elements the array (value of the key "array") will have. Could be very large.

I could set up an endpoint that calls itself (each time converting one json element to xml), until there are no more objects to convert. Would this be the best method?

Solved Solved
1 9 3,095
1 ACCEPTED SOLUTION

adas
New Member

@aleks1

You cannot call any Apigee policy from the custom extensions like javascript or javacallout etc. If you are really intent on doing this, here's what I would suggest:

- Have 2 proxies. Proxy 1 has the JSON-to-XML transformation and Proxy 2 is your regular proxy which has all the logic.

- From within a javascript policy in Proxy 2, you can make calls to Proxy 1 using http client with the required payload. This would be like making a service callout to any other API endpoint and you can write your loop construct in the javascript policy which makes calls to the proxy 1 endpoint and aggregates the response.

Note that this would have its own overhead since you are making calls to another proxy endpoint each time. You could further optimise this using "proxy-chaining" which would allow the extra hops to be removed while making calls to another api proxy endpoint hosted in Apigee. Please see details here or refer to our docs about proxy chaining.

View solution in original post

9 REPLIES 9