Skip to main content
1-Visitor
March 12, 2020
Solved

Filtering the get rest api and returning only the filtered data

  • March 12, 2020
  • 4 replies
  • 2593 views

Hi,

I am using get rest api call and fetching a set of Id and Name. I wanted to pass an argument or filter using the Id field and fetch the data for that respective id alone. How can I acheive this in thingworx javascript? I have mentioned my get service below.

 


var headers = { "Content-Type": "application/json"};

var url = "http://"+ me.host + ":" + me.port + "/api/parametersets";

var params = {
proxyScheme: undefined /* STRING */,
headers: headers /* JSON */,
ignoreSSLErrors: undefined /* BOOLEAN */,
useNTLM: undefined /* BOOLEAN */,
workstation: undefined /* STRING */,
useProxy: undefined /* BOOLEAN */,
withCookies: undefined /* BOOLEAN */,
proxyHost: undefined /* STRING */,
url: url /* STRING */,
timeout: 15 /* NUMBER */,
proxyPort: undefined /* INTEGER */,
password: undefined /* STRING */,
domain: undefined /* STRING */,
username: undefined /* STRING */
};

try {
var json = Resources["ContentLoaderFunctions"].GetJSON(params);

var param1s = {
infoTableName : "InfoTable",
dataShapeName : "FTMParameterSetDS"
};

var newInfo = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(param1s);
var value = json.array.length;

for(var i=0; i<json.array.length; i++)
{
if (json.array[i].Id != null && json.array[i].Id !== "" && json.array[i].Id !=" ")
{
if (json.array[i].Name != null )
{
newInfo.AddRow({Id:json.array[i].Id,
Name:json.array[i].Name});
}
}
}
result = newInfo;
}
catch(err)
{
me.CloseSession();
}

I have attached my output for this get api in which I want to filter w.r.t Id column.

 

Thanks in advance,

svisveswaraiya.

 

 

Best answer by PaiChung

There isn't a way to combine it with Query, but if you look in the InfoTableFunctions you'll find Distinct as a service as well.

4 replies

22-Sapphire I
March 12, 2020

You can do an additional Query on the infotable using the InfoTable functions

Here are all the queries that you can use.

http://support.ptc.com/help/thingworx_hc/thingworx_8_hc/en/index.html#page/ThingWorx%2FHelp%2FComposer%2FThings%2FThingServices%2FQueryParameterforQueryServices.html

 

1-Visitor
March 12, 2020

Hi @PaiChung,

Thanks for your swift response. I tried using LIKE operation from the link and it worked for me. Is there any query to fetch only distinct values from get rest api call. For eg: I want to fetch only distinct column 'Name'.

 

PaiChung22-Sapphire IAnswer
22-Sapphire I
March 12, 2020

There isn't a way to combine it with Query, but if you look in the InfoTableFunctions you'll find Distinct as a service as well.