cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

Search through array of JSON objects

lmooreuk
3-Visitor

Search through array of JSON objects

Hi all,

I am making a GET API call from a ThingWorx service. I want to parse the JSON data from the API call and search through it for certain values, which will then be pushed into an array. It seems to me that standard JavaScript methods like .filter() and .find() don't work in ThingWorx.

Can this be done in the same service or will I need multiple?

Any help would be appreciated.

 

1 ACCEPTED SOLUTION

Accepted Solutions
jensc
17-Peridot
(To:lmooreuk)

Hello,

 

You can find some information here.

Thingworx does support arrow functions, so if you are comfortable using those you can go ahead with that.

 

If your data looks similar to this:

{array:[

{key:value},{key:value},{key:value},{key:value}

]}

Then you can use the forEach() function to loop through the objects in your array, something like this:

array.forEach(object => {
// do your filtering here and array push here


});

 

Hope this helps,

 

Regards,

Jens

View solution in original post

4 REPLIES 4
jensc
17-Peridot
(To:lmooreuk)

Hello,

 

You can find some information here.

Thingworx does support arrow functions, so if you are comfortable using those you can go ahead with that.

 

If your data looks similar to this:

{array:[

{key:value},{key:value},{key:value},{key:value}

]}

Then you can use the forEach() function to loop through the objects in your array, something like this:

array.forEach(object => {
// do your filtering here and array push here


});

 

Hope this helps,

 

Regards,

Jens

Hello,

 

Services like find and filter work in ThingWorx, including ContentLoaderFunctions calls. This example works for me:

 

let json = Resources["ContentLoaderFunctions"].GetJSON({
	url: 'https://datausa.io/api/data?drilldowns=Nation&measures=Population'
});

let result = {	// JSON
    'filtered': json.data.filter(row => (row['ID Year'] < 2015))
};

 

The only related non-intuitive thing I know is that ThingWorx doesn't allow plain arrays as JSON objects and will always wrap them in "array" element, i.e.:

 

// Service return type is JSON
let result = [1, 2, 3]; // Will actually return {"array": [1, 2, 3]}

 

/ Constantine

Hello Constantine,

 

Whenever I try and run a service that uses .filter(), I receive this error:

Error executing service Get_IR_APIs. Message :: TypeError: Cannot call method "filter" of undefined - See Script Error Log for more details.

It is also worth mentioning, while searching for the JSON objects in the array of JSON objects, I want to search for values using the value part, of the key and value pair structure.

 

Thank you

Can you share the complete piece of code which doesn't work?

Top Tags