Layer7 API Management

 View Only
  • 1.  Modify reponse JSON fields

    Posted Dec 06, 2022 06:14 AM
    Hi all,

    the need is to modify responses (JSON format), by applying a mathematical expression to some fields.

    The developed solution is the following:
    For each field in the response to be modified (we have a list of the fields to be modified)
    - "Evaluate JSON expression" to find all occurrences of the field (could be even hundreds with different values for one field)
    - for each occurrence found, apply the "math expression" to obtain the new value
    - for each occurremce set the new value with "regular expression"

    This approach increases the mean response time up to many seconds, as the occurrences to be processed are many hunreds, and it is not acceptable.

    Does someone have an idea about a possible alternative solution to be applyed? The response does not have a fixed schema with fixed fields, so the occurrences and their position can change.

    Thanks in advance!


  • 2.  RE: Modify reponse JSON fields

    Posted Dec 07, 2022 01:30 AM

    if you are using version 9.4 onwards
    //you could use javascript assertion -- jsonbody should be text

    var json = context.getVariable('jsonBody');
    var json = JSON.parse(json);

    // -- after that you use your json manipulation

    //finally save json 


  • 3.  RE: Modify reponse JSON fields

    Posted Dec 07, 2022 01:34 AM

    Option 2:-

    JSON to XML
    --- XPATH manipulation using XSL

    XML to JSON again
    -- or just use xslt to convert the entire body to JSON. This is quicker but need a lot of manipulation