JSON Array with filters
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2023 07:24 AM
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>"}]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2023 08:20 AM
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)
If my answer solved your issue, please mark my answer as ✅ Correct & 👍Helpful based on the Impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2023 09:17 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2023 09:24 AM
@SM16 ,
Can you check this POST ,
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2023 10:23 AM - edited 04-13-2023 10:24 AM
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