BookmarkSubscribeRSS Feed
Resa
Pyrite | Level 9

I have recently started to make some reports about the reports within our Viya 4 environment using the documentation I could find and have basically succeeded to retrieve the information we need. However, what I have not been able to find is whether there is a way to limit the items returned when doing an API call.

 

For example within the  PI Web API it is possible to use the "selectedFields" parameter. I have not been able to find a similar parameter within the documentation for the SAS Viya REST API, therefor my question: Is such a parameter available?

 

For example to use an URL like: &BASE_URI/folders/folders/&uri/members?start=0&limit=10&selectedFields=items.name,items.id
Which would give me only the name and id field from the items "table"

3 REPLIES 3
ronald297
Calcite | Level 5

It is understandable that you would like to limit the items returned in an API call to streamline the data and improve efficiency. In SAS Viya REST API, there is no direct equivalent to the "selectedFields" parameter like you might find in other APIs such as the PI Web API.

However, you can achieve similar results by making use of the filtering and querying capabilities that the SAS Viya REST API offers. While it might not be as straightforward as using a "selectedFields" parameter, you can retrieve specific fields by processing the response data after making the API call.

For instance, you can make a general API call to retrieve all the data and then programmatically filter out the unwanted fields in your code. Here's an example approach using Python to make an API call and filter the results:

import requests
import json

import requests
import json

# Define your base URI and endpoint
BASE_URI = 'https://picoworkers.net/ api/v1/folders'
uri = 'your-folder-uri' # Adjust this to the specific folder URI you are querying
url = f'{BASE_URI}/{uri}/members?start=0&limit=10'

# Make the API call
response = requests.get(url, headers={'Authorization': 'Bearer your-access-token'})

# Check if the request was successful
if response.status_code == 200:
data = response.json()

# Filter the results to include only the 'name' and 'id' fields
filtered_items = [{'name': item['name'], 'id': item['id']} for item in data['items']]

# Print the filtered results
print(json.dumps(filtered_items, indent=2))
else:
print(f"Error: {response.status_code} - {response.text}")

Resa
Pyrite | Level 9

Thank you @ronald297 for your reply.

 

Currently my approach is as you suggest, albeit in SAS code. I used a JSON libname statement and filter out the information that I need.

 

Although in some cases, for example when you want to retrieve groups and their members, I now have to make two API calls. The first to get the required information about the group, the second to get the requirec information about the members in that group. 

Being able to do this within one API call would be nice.

 

Maybe a nice feature to be considered for a future release of the REST API ... selectedFields? 😉

ronald297
Calcite | Level 5

 

In the SAS Viya REST API, there's no direct equivalent to the selectedfields parameter found in the PI Web API. However, you can control the amount of data returned in a few different ways:

Pagination

You can use the start and limit parameters to control which subset of results you receive. For example  https://tvnama.com/ 

This request retrieves the first 10 members from the specified folder.

Filtering and Querying

Some endpoints allow you to use query parameters to filter results. For instance, you can use filter to retrieve specific items based on certain criteria.

Using Specific API Endpoints

Certain API endpoints might have specific query parameters that limit or control the data returned. Check the documentation for each endpoint to see if it supports filtering or selecting specific fields.

Customizing Data Retrieval

If you need specific fields or a more customized response, you might have to retrieve the data in a broader query and then process or filter the data client-side, i.e., in your application or script. For example:

  1. Retrieve the full data set.
  2. Parse the response and extract only the fields you need.

 

To get a limited number of members, use:

 

bash
 

After retrieving the response, you would manually extract the fields name and id from each item in the response payload.

Practical Tips

  1. Check the API Documentation: For each endpoint you use, refer to the SAS Viya REST API documentation to see available parameters and capabilities.
  2. Client-side Processing: Sometimes, it’s more efficient to handle field selection and filtering in your application after receiving the data.

Example Workflow

Here's a simplified example workflow for handling the API response if you can't limit fields directly:

  1. Make API Call:

  2. Process Response: Extract and format the required fields from the JSON response in your application code.

If SAS introduces new API features or if there are updates to the documentation, it would be worthwhile to revisit the documentation or reach out to SAS support for any advanced features or newer capabilities.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2212 views
  • 2 likes
  • 2 in conversation