Layer7 API Management

  • 1.  Dynamically add nodes in JSON message

    Broadcom Employee
    Posted Dec 08, 2015 09:41 AM

    Hi folks

     

    Here is my use-case : my JSON message coming from backend contains some business deals. Each one can be expressed in its own currency.

    So depending on localization of consumer, I have to add a node below each offer currency indicating its exchange rate relative to euro. Each exchange rate is recovered by calling another gateway service.

     

    My JSON is pretty huge,so I can't built it by my own using the evaluate JSON path assertion.

     

    Many thanks if you can help me find the right way to do this !



  • 2.  Re: Dynamically add nodes in JSON message
    Best Answer

    Posted Dec 10, 2015 09:52 AM

    A regular expression that is written to target the appropriate area of the JSON request is the only method that I can think of. I do not believe that we have an assertion other than that one that can modify a JSON message in a manner akin to the Add or Remove XML Element assertion. It may be worthwhile to consider opening an Idea to request that the Add or Remove XML Element assertion be enhanced to handle JSON, as well



  • 3.  Re: Dynamically add nodes in JSON message

    Broadcom Employee
    Posted Dec 18, 2015 12:43 AM

    In theory, you can transform from JSON to XML, then you can apply XSLT, or Add or remove XML element, ... Then transform back to JSON.



  • 4.  Re: Dynamically add nodes in JSON message

    Posted Dec 28, 2015 07:16 AM

    It is worth noting that this will have significant transactional overhead. A single regular expression is much faster than two transformations.



  • 5.  Re: Dynamically add nodes in JSON message

    Broadcom Employee
    Posted Dec 28, 2015 07:29 PM

    Exactly, it's 3 transformations if use XSLT.

    Surely Regex is much faster, but in some cases, it might not be easy to write a Regex, then XSLT should help.