Layer7 API Management

  • 1.  validate structure of JSON

    Posted Jan 03, 2018 08:16 AM

    I have to validate the structure of the JSON coming in the request body so that it should contain some specific keys. How we can achieve this in API Gateway?



  • 2.  Re: validate structure of JSON
    Best Answer

    Broadcom Employee
    Posted Jan 03, 2018 09:53 AM
      |   view attached

    Hi Ashutosh,

     

    You can use the Validate JSON Schema assertion documented here: Validate JSON Schema Assertion (Message Validation/Transformation) - CA API Gateway - 9.2 - CA Technologies Documentatio… 

     

    You can define the schema to have required fields, ie:

     

    {
    "type": "object",
    "properties": {
    "name": {"type": "string"},
    "email": {"type": "string"},
    "company": {"type": "string"}
    },
    "required": [
    "name",
    "email",
    "company"
    ],
    "additionalProperties": false
    }

     

    REQUEST

    {
    "name": "Joe",
    "email": "user@email.com",
    "company": "CA Tech"
    }

     

    I have attached a very simple policy to demonstrate this.

     

    Example with the required fields

     

    Example without the required fields

    Regards,

    Joe

    Attachment(s)

    zip
    validate_json.xml.zip   693 B 1 version


  • 3.  Re: validate structure of JSON

    Broadcom Employee
    Posted Jan 05, 2018 10:32 AM

    Joe

    I am using API Gateway 9.2 CR05, I have done this, however for some reason, API Gateway always considers everything in the properties as mandatory, It is not allowing optional values, though I am trying to include only few required values. However the same works perfectly fine using the JSON Schema Validator - Newtonsoft 

     

    Here's the screen shot showing that I have only name and company as required and the email is optional, however API Gateway still expects that email must exist:

    My JSON Schema:

    { "type": "object", "properties": { "name": {"type": "string"}, "email": {"type": "string"}, "company": {"type": "string"} }, "required": [ "name", "company" ], "additionalProperties": false }

    Screen Shot:

    JSON Schema from the Gateway

    Request:

    { "name":"asdfdf", "company":"asfsdfd" }

    Response from Gateway still expecting email though it is not a required attribute:

     

    HTTP/1.1 200 OK
    Server: Apache-Coyote/1.1
    Content-Encoding: gzip
    Content-Type: text/plain;charset=UTF-8
    Content-Length: 67
    Date: Fri, 05 Jan 2018 15:05:01 GMT

    Failure: $.email: is missing and it is not optional

     

    Gateway Response showing that it still expects email as Required though the schema does not include the same as Required.

     

    Here's the output from JSON Schema Validator tool showing the same schema works here:

     

     

    JSON Schema Validator Response showing that the JSON schema is correct and it is working as it should

     

    Could you please advise on why the validation is failing on Gateway



  • 4.  Re: validate structure of JSON

    Broadcom Employee
    Posted Jan 05, 2018 10:52 AM

    Muthu,

     

    I would check it against Gateway 9.3. The site you provided is validating with JSON Schema 3+. 9.2 Gateway does not support this. however 9.3 added support for schema 4.

     

    Regards,

    Joe



  • 5.  Re: validate structure of JSON

    Posted Jan 07, 2018 11:58 PM

    Hi Muthu,

     

    If you are using gateway 9.2 CRx, it is using the v2 of the json schema validation.

     

    You can refer to the following site for the syntax of the json schema.

    I would believe the attribute you are looking for is optional

     

    draft-zyp-json-schema-02 - JSON Schema: core definitions and terminology