How could i get json from com.apigee.flow.message.MessageContext?

Not applicable

I need to parse requested json before proceed further, i am using javacallback, but not sure how to handle with JSON...

Solved Solved
0 4 1,908
2 ACCEPTED SOLUTIONS

Not applicable

You will always get a String representation of the variable when you try getVariable method. Now you can use another java library to convert to / parse as JSON objects as needed.

If I understand correctly, your payload is a JSON request. If you are interested in specific elements of the JSON, you can always use a JSONPath extraction policy and assign specific elements of the JSON as a variable in your flow. This might save you the effort of writing a java callout.

However, when your payload JSON has array and you may need to loop through the payload object, java callout or javascript is a good option.

JavaScript is particularly good for handling JSON because you do not need any additional library to handle JSON, unlike in Java. JSON is native to JavaScript. So, net-net, here is my suggestion. 1. Try using JSONpath variable extraction if you are looking for a non-array JSON in the payload. It is easier that way. 2. Otherwise write a JavaScript that would reduce programming effort for the JSON part.

View solution in original post

Not applicable

When I try to iterate the JSON object in Javascript, the EDGE throws error as JSON iteration blocked. I tried it in Cloud version. Anyone has this similar issues?

View solution in original post

4 REPLIES 4

Not applicable

You will always get a String representation of the variable when you try getVariable method. Now you can use another java library to convert to / parse as JSON objects as needed.

If I understand correctly, your payload is a JSON request. If you are interested in specific elements of the JSON, you can always use a JSONPath extraction policy and assign specific elements of the JSON as a variable in your flow. This might save you the effort of writing a java callout.

However, when your payload JSON has array and you may need to loop through the payload object, java callout or javascript is a good option.

JavaScript is particularly good for handling JSON because you do not need any additional library to handle JSON, unlike in Java. JSON is native to JavaScript. So, net-net, here is my suggestion. 1. Try using JSONpath variable extraction if you are looking for a non-array JSON in the payload. It is easier that way. 2. Otherwise write a JavaScript that would reduce programming effort for the JSON part.

Not applicable

When I try to iterate the JSON object in Javascript, the EDGE throws error as JSON iteration blocked. I tried it in Cloud version. Anyone has this similar issues?

If you have another question, I suggest posting it as a new question, rather than as an answer to your prior question.

Also, when posting your question, show the actual error. You said "Edge throws error". You'll want to describe EXACTLY the error that you see, and how you obtained it. Providing all the information you have available, will be the best approach to getting people to help you.

Not applicable

Hi @Paranitharan,

Not sure exactly what you mean by "JSON iteration blocked," but here's a simple Javascript that shows how to iterate through a JSON object. You can add that to the Javascript file associated with your Javascript policy in Apigee Edge and try it out:



    var jsonBody = context.targetRequest.body.asJSON
    traverse(jsonBody)

    // recursive function to iterate through JSON object
    function traverse(o) {
      if (o != null && typeof o == "object") {    
        for (var k in o) {
          var v = o[k];
          if(v != null && typeof v == "object") {
            print("key: " + k)
            traverse(o[k])
          } else {
            print("key: " + k + ", value: " + v)
          }
        }
      }
    }

You may also have run into the 200 ms execution time limit for Javascript on Apigee Developer as documented here in case you have similar code as above, but it is taking longer than 200 ms to execute.