Layer7 API Management

 View Only
  • 1.  JSON output update

    Posted 25 days ago

    I am getting response in JSON format from an external service. But I was add few attributes to this JSON and also format few values in this JSON as required before returning the response to call. How I can add new elements in JSON object and also update existing element values? 



  • 2.  RE: JSON output update

    Posted 24 days ago
    • First, store the backend response in a context variable - usually from ${response.mainpart} after routing.

    • Then use the Evaluate JSON Path assertion to pull out the values you need from the JSON and save them into individual context variables.
      You'd find Evaluate JSON Path assertion here: https://techdocs.broadcom.com/us/en/ca-enterprise-software/layer7-api-management/api-gateway/11-1/policy-assertions/assertion-palette/message-validation-transformation-assertions/evaluate-json-path-expression-assertion.html

    • After that, use a Set Variable assertion (set to type "Message") to build your new JSON response using those extracted values. You can also add any extra info you want here. Name this variable something like jsonresponse.

    • Finally, return that constructed JSON back to the client using the Return Template Response to Requestor assertion and refer to the jsonresponse variable.




  • 3.  RE: JSON output update

    Posted 24 days ago

    Thank you, Bala for your response. Such approach makes more hard and error prone to implement when you have complex JSON structure. I am looking for Javascript DOM like ability to process JSON easily using any assertion which i think it is not available currently.




  • 4.  RE: JSON output update

    Posted 24 days ago

    Hey Shamim, Totally get your point, working with complex JSON structures using JSON Path and context variables can feel a bit clunky at times. But we've actually used this method for some pretty complex JSON responses, and it's worked perfectly for us when done carefully.

    It does require a bit of attention to detail, but once everything is mapped correctly, it's a solid approach. Hopefully, Layer7 will offer more flexible JSON processing in the future to make things even easier for use cases like this.




  • 5.  RE: JSON output update

    Broadcom Employee
    Posted 24 days ago

    Hello Shamim,

    You can also process JSON using the Javascript assertion, or you may want to have a look at our experimental JSON Patch assertion. Depending on customer feedback and demand, we may prioritize advancing the latter to preview soon.

    Regards,



    ------------------------------
    Ben Urbanski
    Product Manager, API Gateway
    Layer7 API Management
    ------------------------------



  • 6.  RE: JSON output update

    Posted 24 days ago

    I agree with Ben!
    The only thing with Javascript assertion and JSON objects might be the formatting of the output JSON.
    If you have large numbers, floating point numbers in the input JSON, you may get different formatting after processing with Javascript assertion. (get scientific notation for large numbers, loose the precision for some values. E.g.: 4.50 --> 4.5)



    ------------------------------
    Mesut Yalcin
    Senior Solution Architect
    MAXXYS AG - http://www.maxxys.de
    ------------------------------