Layer7 API Management

  • 1.  How to manipulate JSON response payload to modify/add/delete fields?

    Posted Feb 21, 2017 01:48 PM

    Is there a policy assertion or technique I can use efficiently to manipulate (modify existing attributes, or add/delete attributes) JSON response data to give back to the requestor?  

     

    Given:

     

    original response:
    {

      "name": "First Last",

      "address": "123 Address Dr, Paris, TX",

      "social": "123-45-6789"

    }

     

    Desired:

    updated response - modify existing attribute:

    {

      "name": "First Last",

      "address": "123 Address Dr, Paris, TX",

      "social": "***********"

    }

     

    updated response - add:

    {

      "name": "First Last",

      "address": "123 Address Dr, Paris, TX",

      "social": "123-45-6789",

      "new_attribute": "my new value"

    }

     

    updated response - delete:

    {

      "name": "First Last",

      "address": "123 Address Dr, Paris, TX"

    }

     

    I've looked at the JSONParse and the XMLParse assertions but they are used to convert from XML to JSON and vice versa.  We also looked at policies where u could extract key values out of the JSON response to set as a value.  Is there a way other than having to reconstruct the JSON response attribute-by-attribute to create any of the desired responses?

     

    Thanks,

    Hoang



  • 2.  Re: How to manipulate JSON response payload to modify/add/delete fields?
    Best Answer

    Posted Feb 21, 2017 03:02 PM

    Hi,

    I have included a few techniques you can try for this use case:

     

    1. Modify Existing attributes to replace elements in the JSON body.
      Use the Evaluate Regular Expression to replace attributes
    2. Add/Delete Attributes.
      First use the Apply JSON Transformation to convert the message to XML, then use the Add or Remove XML Element(s) Assertion to add/remove elements from the document then reapply the JSON Transformation to transform from XML to JSON.


  • 3.  Re: How to manipulate JSON response payload to modify/add/delete fields?

    Broadcom Employee
    Posted Feb 21, 2017 04:36 PM

    Good thinking Mohindra.Singh.

     

    For item 1 if you are unsure of the value or pattern to match (not always a SS #) you can use the 'Evaluate JSON Path Expression' to retrieve the value and feed it to the RegEx to replace.

     

    Evaluate JSON Path Expression Assertion - CA API Gateway - 9.1 - CA Technologies Documentation 

     

    Regards,

    Joe