Validate JSON payload in request

I have an API which expects a JSON payload in the POST request. Now I need to extract some variables from the payload which I am doing using an extract variables policy.

However, if the JSON is not valid, the extract variables policy fails (and I don't really get to know that it failed due to a bad JSON).

Is there a policy I can use to check the JSON validity? Will the JSON Threat Protection policy help?

Solved Solved
3 7 6,881
1 ACCEPTED SOLUTION

Dear @Prashanth Subrahmanyam ,

You can use Message Validation Policy. It works, I tested against JSON. If a <ResourceURL> value is not specified, the message is checked for well-formed JSON if the content-type is application/json.

See failure & success responses below....

Failure :

{"fault": {"faultstring": "SOAP-Message-Validation-1 failed with reason: \"Expecting { or [ at line 1\"","detail": {"errorcode": "steps.messagevalidation.Failed"}}}

Success :

// prints json

You can also use JS Policy as explained here..

Cheers,

Anil Sagar

View solution in original post

7 REPLIES 7

Dear @Prashanth Subrahmanyam ,

You can use Message Validation Policy. It works, I tested against JSON. If a <ResourceURL> value is not specified, the message is checked for well-formed JSON if the content-type is application/json.

See failure & success responses below....

Failure :

{"fault": {"faultstring": "SOAP-Message-Validation-1 failed with reason: \"Expecting { or [ at line 1\"","detail": {"errorcode": "steps.messagevalidation.Failed"}}}

Success :

// prints json

You can also use JS Policy as explained here..

Cheers,

Anil Sagar

Thanks @Anil Sagar. Yes, I came across the SO answer but it was 1 year old. 🙂

So nothing has changed, and JS is still the best way to go?

Dear @Prashanth Subrahmanyam ,

After doing some research found that SOAP Message Validation policy might solve the issue. Documentation says that,

If a <ResourceURL> value is not specified, the message is checked for well-formed JSON if the content-type is application/json.

Can you check same ?

Cheers,

Anil Sagar

Used this policy. Works perfectly. Thanks a lot. 🙂

Awesome @Prashanth Subrahmanyam , Glad issue is resolved.

Thank you for the points reward 😉

Check updated answer 🙂

@Anil Sagar, @Prashanth Subhrahmanyam Can we use SOAP Validation Policy to validate the json against json schema? ie., not only well-formness, can it also validate if the json has required mandatory elements, data types, restrictions etc?