Layer7 API Management

Expand all | Collapse all

Validate JASON input structure only

Jump to Best Answer
  • 1.  Validate JASON input structure only

    Posted 03-05-2020 05:06 AM
    Want to validate JSON input structure only. Requirement is if the input is not in proper JSON format then have to throw custom error message. line"The input is not JSON format"
     Input is like below:
    {
    "input" : {
    "EmailId" : "abc",
    "Flag" : "ee",
    "RecordId" : "55"
    }
    }

    N.B:Schema validation not required.


  • 2.  RE: Validate JASON input structure only
    Best Answer

    Posted 03-05-2020 10:40 AM
    Hi Anirban,

    For Gateway 9.4, the JavaScript assertion can be used to validate JSON structure. Prior to 9.4, I am thinking maybe the Evaluate JSON Path Expression assertion v2.
    In the latter case, setup a simple expression, $, inside an at least one branch

    At least one
      -Evaluate JSON Path
      -set result to invalid JSON

    If there are 0 or more results, the Evaluate statement passes, otherwise , if something goes wrong, return the invalid JSON message.

    For JavaScript

    var json = context.getVariable ('request.mainpart');
    try {
       JSON.parse(json);
       context.setVariable("result","valid JSON");
    } catch (e) {
       context.setVariable("result","invalid JSON");
    }