JSON Array with filters

SM16
Tera Expert

Hi Team,

 

I have an endpoint that accepts filter as JSON Array with filters as stated in their documentation. How can I use that to build the endpoint?

 

This is what they gave me. Endpoint is https://myAppliance.hostname.com/rest/scan/solution?limit=5

 

JSON array with filters; [{"field":"<name>","value":"<string/number/boolean>","comparison":"<eq/ne/gt/lt/any/all/none/today/null>"}]

 

4 REPLIES 4

Prince Arora
Tera Sage
Tera Sage

@SM16 

 

Your question is not complete, their might be some API guide available for your 3rd party, please check the syntax in that how to pass JSON filters via the endpoint.

 

Generally we pass the JSON object in the body of the request (POST/PUT)

 

https://www.servicenow.com/community/developer-forum/how-to-pass-variables-to-rest-message-request-b... 

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

I don't want to pass anything to inject to a third party. It's passed as a parameter to the endpoint. Check here from the documentation

 

SM16_0-1681402607523.png

 

@SM16 ,

 

Can you check this POST ,

 

I would suggest to pass the JSON data in the body as a POST request. But if you still want to pass this as a parameter in URL, you will have to encode your URL like below just for example:-

 

for ex json is :->{"name":"ABC","id":"1"}

testurl:80/service?data=%7B%22name%22%3A%22ABC%22%2C%22id%22%3A%221%22%7D

 

 

So please make a JSON object and then append it with URL and encode it!

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Amit Gujarathi
Giga Sage
Giga Sage

Hi @SM16 ,
I trust you are doing good.

To build an endpoint that accepts JSON array with filters, you can use ServiceNow's REST API capabilities. You can define a REST API endpoint by creating a Scripted REST API in ServiceNow, which allows you to define the URL, HTTP method, request and response formats, and the script that handles the request.

In your case, you can define a GET request for the endpoint https://myAppliance.hostname.com/rest/scan/solution and accept the filters as query parameters. Then, in the script that handles the request, you can parse the query parameters and build a query based on the JSON array with filters.

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // Get the filters from the query parameters
    var filters = request.queryParams.filters;

    // Parse the filters as a JSON array
    var filtersArray = JSON.parse(filters);

    // Build the query based on the filters
    var query = '';
    for (var i = 0; i < filtersArray.length; i++) {
        var filter = filtersArray[i];
        var field = filter.field;
        var value = filter.value;
        var comparison = filter.comparison;

        // Build the query based on the filter
        if (i > 0) {
            query += '^';
        }
        query += field + comparison + value;
    }

    // Build the full URL with the query parameter
    var url = 'https://myAppliance.hostname.com/rest/scan/solution?limit=5&sysparm_query=' + encodeURIComponent(query);

    // Send a request to the external endpoint
    var gr = new GlideRecord('u_external_api');
    gr.setValue('u_url', url);
    gr.get();
    var result = gr.getValue('u_result');

    // Set the response body to the result from the external endpoint
    response.setBody(result);

})(request, response);

 


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi