# Extract information from API

**URL:** <https://discourse.nodered.org/t/extract-information-from-api/34120>\
**Category:** General\
**Created:** [12 October 2020 11:08 UTC](https://discourse.nodered.org/t/extract-information-from-api/34120 "2020-10-12T11:08:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanTimu](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@DanTimu](https://discourse.nodered.org/u/DanTimu)\
**Post date:** [12 October 2020 11:08 UTC](https://discourse.nodered.org/t/extract-information-from-api/34120/1 "2020-10-12T11:08:12Z")

</div>

Hi, I'm trying to extract data from a GET request from the following site:  
[https://cluj-napoca.pulse.eco/rest/current](https://cluj-napoca.pulse.eco/rest/current)

I only need the information from a specific sensorId.  
For example I need the info from: 522ad249-96b8-47b7-a63e-8dba638cef6e and the output payload to display PM10, PM25, Humidity, Temperature, Pressure, etc.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 October 2020 11:35 UTC](https://discourse.nodered.org/t/extract-information-from-api/34120/2 "2020-10-12T11:35:08Z")

</div>

Welcome to the forum  
That looks like JSON data, feed it into a JSON node and it should convert it to a javascript object so you can access the elements you want

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 October 2020 11:37 UTC](https://discourse.nodered.org/t/extract-information-from-api/34120/3 "2020-10-12T11:37:54Z")

</div>

A simple example of how to do http request return a json object and filter for sensorId.

```auto
[{"id":"55f374bb.7d3eb4","type":"inject","z":"7760f563.ea0324","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":2080,"wires":[["318c812a.d34f46"]]},{"id":"318c812a.d34f46","type":"http request","z":"7760f563.ea0324","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://cluj-napoca.pulse.eco/rest/current","tls":"","persist":false,"proxy":"","authType":"","x":280,"y":2080,"wires":[["b77ed1a0.fe285"]]},{"id":"b77ed1a0.fe285","type":"change","z":"7760f563.ea0324","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$filter(payload, function($v){$v.sensorId =\"522ad249-96b8-47b7-a63e-8dba638cef6e\"})","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":2060,"wires":[["e1e90f78.6aa54"]]},{"id":"e1e90f78.6aa54","type":"debug","z":"7760f563.ea0324","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":2060,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![DanTimu](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@DanTimu](https://discourse.nodered.org/u/DanTimu)\
**Post date:** [12 October 2020 15:59 UTC](https://discourse.nodered.org/t/extract-information-from-api/34120/4 "2020-10-12T15:59:38Z")

</div>

It looks good, but I need to join all the filtered information into a single message. What I want to do, is to get info for pm25, pm10 into an message like: pm25: "value", pm10: "value"

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 October 2020 16:05 UTC](https://discourse.nodered.org/t/extract-information-from-api/34120/5 "2020-10-12T16:05:05Z")

</div>

look at the template node - that is designed to help you do that.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 October 2020 16:43 UTC](https://discourse.nodered.org/t/extract-information-from-api/34120/6 "2020-10-12T16:43:07Z")

</div>

Or do it in the change node, template will be more readable.

```auto
[{"id":"6a64d05.3bc4a3","type":"change","z":"7760f563.ea0324","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$merge(\t $map(\t $filter(\t payload,\t function($v){\t $v.sensorId =\"522ad249-96b8-47b7-a63e-8dba638cef6e\"\t }\t)\t,\t function($v){{$v.type: $v.value}}\t )\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":1000,"wires":[["2eab5727.a113f"]]}]

```

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [12 October 2020 16:57 UTC](https://discourse.nodered.org/t/extract-information-from-api/34120/7 "2020-10-12T16:57:15Z")

</div>

Here is a very similar flow using a Function node

```auto
[{"id":"efaea6e1.c7a46","type":"inject","z":"d345a010.8f36d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":180,"wires":[["72bef589.4425dc"]]},{"id":"72bef589.4425dc","type":"http request","z":"d345a010.8f36d8","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://cluj-napoca.pulse.eco/rest/current","tls":"","persist":false,"proxy":"","authType":"","x":490,"y":180,"wires":[["c07a7573.a818c8"]]},{"id":"c07a7573.a818c8","type":"function","z":"d345a010.8f36d8","name":"","func":"let arr = msg.payload\nlet pm25 = arr.find(el => el.sensorId == '522ad249-96b8-47b7-a63e-8dba638cef6e' && el.type == 'pm25')\nlet pm10 = arr.find(el => el.sensorId == '522ad249-96b8-47b7-a63e-8dba638cef6e' && el.type == 'pm10')\n\nlet newMsg = {\n \"payload\": arr,\n \"pm25\" : pm25.value,\n \"pm10\" : pm10.value\n};\n\nreturn newMsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":680,"y":180,"wires":[["4f633983.ecbeb"]]},{"id":"4f633983.ecbeb","type":"debug","z":"d345a010.8f36d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":850,"y":180,"wires":[]}]

```

ps. i noticed that your **sensorId** `522ad249-96b8-47b7-a63e-8dba638cef6e` doesnt return  
Humidity, Temperature, Pressure ? Are those stored under a different device id ?

ps2. Does your API always return **one** PM25 and PM10 respectively with that id ?

---

<div class="post-metadata">

**Author:** ![DanTimu](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@DanTimu](https://discourse.nodered.org/u/DanTimu)\
**Post date:** [12 October 2020 17:54 UTC](https://discourse.nodered.org/t/extract-information-from-api/34120/8 "2020-10-12T17:54:17Z")

</div>

522ad249-96b8-47b7-a63e-8dba638cef6e was an example, also this API address returns the current readings from the sensors

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 October 2020 17:54 UTC](https://discourse.nodered.org/t/extract-information-from-api/34120/9 "2020-10-26T17:54:29Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
