We are in the process of trying to retrieve information from the SAS Viya environment by using the REST API. In order to make my question clear I will use in this example that we are trying to retrieve the name, description, type, memberCount and parentFolderUri from all folders.
In the current situation when we make the initial call, the following information is returned:
{ "version": 2, "accept": "application/vnd.sas.content.folder", "count": 943, "start": 0, "limit": 20, "name": "folders", "items": [{ "creationTimeStamp": "2022-11-07T15:23:51.213681Z", "createdBy": "who.ever@somecompany.com", "modifiedTimeStamp": "2022-11-07T15:23:51.213684Z", "modifiedBy": "who.ever@somecompany.com", "version": 1, "id": "0016a0ee-2461-46f8-a794-0267ee63f65f", "name": "SomeFolder", "parentFolderUri": "/folders/folders/b12eee80-32a4-42f0-a905-272742be7dae", "description": "Some Folder Description", "type": "folder", "memberCount": 6, "links": [{ "method": "GET", "rel": "self", "href": "/folders/folders/0016a0ee-2461-46f8-a794-0267ee63f65f", "uri": "/folders/folders/0016a0ee-2461-46f8-a794-0267ee63f65f", "type": "application/vnd.sas.content.folder"
Basically all information for the first 20 folders is returned while I only want limited information returned. Although sufficient information is provided that I can achieve what I want I would like is to do this more directly.
In the Osisoft PI REST API there is the possibility to select the fields that are being returned. Basically (to keep it in SQL terms) it provides the possibility to not only do:
select * from ...
but to change this into:
select column1, column5 from ...
Basically what I am looking for is to start out with (NOTE: the selectedFields is how it is done for PI):
https://example.com/folders/folders?selectedFields=count
This would return me only the value: 943
After this I could loop using the "column filter" plus the start and limit parameters to retrieve the required information for each folder using:
https://example.com/start=x&limit=20&selectedFields=Items.name;Items.description;Items.type;Items.memberCount;Items.parentFolderUri
Giving me hereby only the specific information I would like to have.
Now to get to my specific question: Is there a SAS Viya REST API equivalent for the "selectedFields" as used in the example?
Hi @Resa,
Thanks for your inquiry. You have several options here. Credit to @Mike_Drutar for his fact checking my response.
First, no, I don't believe there is a mechanism to select fields returned in the response. Most APIs I’ve worked with function similarly. What you can do is parse the response and we have LOTS of options there. As for the default number of 20 items returned in the response, you can use the limit=x parameter in your call to return more data.
If you are making the API call from SAS using proc http, you can store the response in a file and use the JSON libname engine to convert the response as a nicely formatted dataset - and then use actual SQL to keep or drop any variables.
Pretty neat and easy, huh?
If you’re using a different client, say Postman, you can put the response contents in a json file, load it into SAS and again parse using the json libname. An example is:
filename response "C:\temp.json"
libname response json;
In SAS Viya, you can upload the json file to a Viya content folder (like "/Public" or "/My Folder"), which lets you reference a file located in a viya content folder. Then use the FILENAME : FILESRVC Access Method. For example:
filename myfldr2 filesrvc folderpath='/Shared Data/Sales' filename='sales.json';
Here’s a more comprehensive example from Mike:
Download the response from http://viyaserver.com/reports/reports (which is a listing of 20 reports) as reports.json
Local SAS (using the local filename statment):
save it to C:\temp\reports.json:
SAS Code:
filename rpt "C:\temp\reports.json";
libname rpt json;
proc print data=rpt.items;
var name createdBy creationTimeStamp modifiedBy;
run;
The SAS Viya platform (using the filesrvc filename statement):
Upload reports.json to a folder (example: /Public):
SAS Code:
filename rpt filesrvc folderpath='/Public/' filename='reports.json';
libname rpt json;
proc print data=rpt.items;
var name createdBy creationTimeStamp modifiedBy;
run;
Please let me know if you have further questions or need more information.
--Joe
Join us for SAS Community Trivia
SAS Bowl XLVIII, All Things Models
Wednesday, February 19, 2024, at 10:00 a.m. ET | #SASBowl
Hi @Resa,
Thanks for your inquiry. You have several options here. Credit to @Mike_Drutar for his fact checking my response.
First, no, I don't believe there is a mechanism to select fields returned in the response. Most APIs I’ve worked with function similarly. What you can do is parse the response and we have LOTS of options there. As for the default number of 20 items returned in the response, you can use the limit=x parameter in your call to return more data.
If you are making the API call from SAS using proc http, you can store the response in a file and use the JSON libname engine to convert the response as a nicely formatted dataset - and then use actual SQL to keep or drop any variables.
Pretty neat and easy, huh?
If you’re using a different client, say Postman, you can put the response contents in a json file, load it into SAS and again parse using the json libname. An example is:
filename response "C:\temp.json"
libname response json;
In SAS Viya, you can upload the json file to a Viya content folder (like "/Public" or "/My Folder"), which lets you reference a file located in a viya content folder. Then use the FILENAME : FILESRVC Access Method. For example:
filename myfldr2 filesrvc folderpath='/Shared Data/Sales' filename='sales.json';
Here’s a more comprehensive example from Mike:
Download the response from http://viyaserver.com/reports/reports (which is a listing of 20 reports) as reports.json
Local SAS (using the local filename statment):
save it to C:\temp\reports.json:
SAS Code:
filename rpt "C:\temp\reports.json";
libname rpt json;
proc print data=rpt.items;
var name createdBy creationTimeStamp modifiedBy;
run;
The SAS Viya platform (using the filesrvc filename statement):
Upload reports.json to a folder (example: /Public):
SAS Code:
filename rpt filesrvc folderpath='/Public/' filename='reports.json';
libname rpt json;
proc print data=rpt.items;
var name createdBy creationTimeStamp modifiedBy;
run;
Please let me know if you have further questions or need more information.
--Joe
Join us for SAS Community Trivia
SAS Bowl XLVIII, All Things Models
Wednesday, February 19, 2024, at 10:00 a.m. ET | #SASBowl
Hi @joeFurbee,
Thanks for the quick response.
Basically answered my question already with your statement:
no, I don't believe there is a mechanism to select fields returned in the response.
I was hoping that (as with the PI REST API) there was a possibility to select the information that you would like to have in the output.
With regard to the solutions you've mentioned. I was and am aware of the various ways to obtain and save the data, that was not my major issue. 😉
Maybe something to consider to include in the API, the possibility to select columns? 🤔
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.