# Neater way of checking ping results

**URL:** https://discourse.nodered.org/t/neater-way-of-checking-ping-results/84343
**Category:** General
**Tags:** javascript
**Created:** [6 January 2024 17:41 UTC](https://discourse.nodered.org/t/neater-way-of-checking-ping-results/84343 "2024-01-06T17:41:36Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![AndrewBeasley](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@AndrewBeasley](https://discourse.nodered.org/u/AndrewBeasley)
#### Post date: [6 January 2024 17:41 UTC](https://discourse.nodered.org/t/neater-way-of-checking-ping-results/84343/1 "2024-01-06T17:41:36Z")

</div>

Trying to get my head around Javascript and Node-Red this year and looking to use a bit more code rather than convoluted flows that stretch across the screen...

I am using the ping function from node-red-node-ping to check a list of devices for network connection and merging the resultant messages back into one using the join node as I know the number of messages returned.

This gives me a JSON object (using test URLs here) like this:

```auto
{"google.com":6.98,"microsoft.com":14.2}

```

If any system is not available I get a boolean false as the ping time:

```auto
{"google.com":7.01,"testsitetofail.com":false}

```

I need to be able to see if any have failed (not fussed which - any is fine as I do not need to know the site name just the 'fail' state) and I've been converting this to a string and using the switch node testing for the text ":false" but feel there is a better Javascript way!

Problem is I have zero idea what to code - in Python I would just scan this as a dictionary but my Javascript is very very weak 🙂 😊

I seen code using `for / break` or `object.entries` but unsure if these work within Node-Red and what is a better way to go.

Extra thanks if you can point me in the direction of a good intro to Javascript for Node-Red tutorial!

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [6 January 2024 17:46 UTC](https://discourse.nodered.org/t/neater-way-of-checking-ping-results/84343/2 "2024-01-06T17:46:02Z")

</div>

> [@AndrewBeasley](#):
>
> This gives me a JSON object (using test URLs here) like this:
> 
> ```auto
> {"google.com":6.98,"microsoft.com":14.2}
> 
> ```

can you share a sample flow that presents this output?

> [@AndrewBeasley](#):
>
> I need to be able to see if any have failed (not fussed which - any is fine as I do not need to know the site name just the 'fail' state) and I've been converting this to a string and using the switch node testing for the text ":false" but feel there is a better Javascript way!

Do you check an array of things or have individual ping nodes and individual results?

Again share your flow

* * *

To share flow, select the nodes of interest, press `ctrl+e`, click copy, paste the reply into a `</>` code block

`````  
`paste flow between 3 backticks like this`  
`````

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [6 January 2024 18:23 UTC](https://discourse.nodered.org/t/neater-way-of-checking-ping-results/84343/3 "2024-01-06T18:23:29Z")

</div>

Using your data

```auto
{"google.com":7.01,"testsitetofail.com":false}

```

and this flow

```auto
[{"id":"b0282b9b2d1118b4","type":"inject","z":"339501fde5391440","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"google.com\":7.01,\"testsitetofail.com\":false}","payloadType":"json","x":350,"y":120,"wires":[["7e523902c1d1a585"]]},{"id":"a9c28b6388aa36cf","type":"debug","z":"339501fde5391440","name":"Payload false","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"parts.key","targetType":"msg","statusVal":"","statusType":"auto","x":760,"y":100,"wires":[]},{"id":"7e523902c1d1a585","type":"split","z":"339501fde5391440","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":470,"y":120,"wires":[["8ee65244ddc8670f"]]},{"id":"8ee65244ddc8670f","type":"switch","z":"339501fde5391440","name":"","property":"payload","propertyType":"msg","rules":[{"t":"false"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":590,"y":120,"wires":[["a9c28b6388aa36cf"],["043137d32b541f24"]]},{"id":"043137d32b541f24","type":"debug","z":"339501fde5391440","name":"Valid payload","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"parts.key","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":140,"wires":[]}]

```

I get this, which might be useful, even without function nodes 😄

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8f8726e5f3ac5b7a2c2e250649470a16caa3f070.png)

---

<div class="post-metadata">

### Author: ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)
#### Post date: [6 January 2024 20:03 UTC](https://discourse.nodered.org/t/neater-way-of-checking-ping-results/84343/4 "2024-01-06T20:03:04Z")

</div>

> [@AndrewBeasley](#):
>
> I need to be able to see if any have failed

If this is the only request, then a switch node with jsonata :

```auto
$contains($string($.*),"false") = true ? "fail" : "success"

```

will give you fail if there is at least one `false` in all the pings.

---

<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: [6 January 2024 20:16 UTC](https://discourse.nodered.org/t/neater-way-of-checking-ping-results/84343/5 "2024-01-06T20:16:40Z")

</div>

That seem like an overly complex JSONata expression

```auto
$not(false in $$.payload.*)

```

will return false if any false pings are returned

or Javascript equiverlent

```auto
msg.payload = !Object.values(msg.payload).some(val => val === false);

```

---

<div class="post-metadata">

### Author: ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)
#### Post date: [6 January 2024 21:14 UTC](https://discourse.nodered.org/t/neater-way-of-checking-ping-results/84343/6 "2024-01-06T21:14:00Z")

</div>

I knew that I was taking a risk in proposing a not so great jsonata method 😉  
The "only" merit of mine (I think), it is a bit less cryptic.  
Sometimes (often ?) jsonata is cryptic. For me at least.

---

<div class="post-metadata">

### Author: ![AndrewBeasley](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@AndrewBeasley](https://discourse.nodered.org/u/AndrewBeasley)
#### Post date: [7 January 2024 12:25 UTC](https://discourse.nodered.org/t/neater-way-of-checking-ping-results/84343/7 "2024-01-07T12:25:38Z")

</div>

Wow, thanks folk - lots of bits to try and get my head around 🙂

A test flow (sorry should have included it) is:

```auto
[{"id":"a3c75a60826c7302","type":"tab","label":"Testing","disabled":false,"info":"","env":[]},{"id":"1a871f9f3426c836","type":"ping","z":"a3c75a60826c7302","protocol":"Automatic","mode":"triggered","name":"","host":"","timer":"20","inputs":1,"x":570,"y":320,"wires":[["f718fbc578ac622b"]]},{"id":"74925663b4591d50","type":"debug","z":"a3c75a60826c7302","name":"Raw Payload","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":440,"wires":[]},{"id":"12131cadced372e8","type":"inject","z":"a3c75a60826c7302","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":320,"wires":[["af20663a3ba2595d"]]},{"id":"af20663a3ba2595d","type":"function","z":"a3c75a60826c7302","name":"function 1","func":"msg.payload = [\n {\n \"host\": \"google.com\",\n \"name\": \"external test point\",\n \"timeout\": 10000,\n },\n {\n \"host\": \"testsitetofail.com\",\n \"name\": \"external fail point\",\n \"timeout\": 10000,\n }\n]\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":320,"wires":[["1a871f9f3426c836"]]},{"id":"f718fbc578ac622b","type":"join","z":"a3c75a60826c7302","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":730,"y":320,"wires":[["74925663b4591d50","7ce0692d711772d4"]]},{"id":"7ce0692d711772d4","type":"json","z":"a3c75a60826c7302","name":"","property":"payload","action":"str","pretty":false,"x":910,"y":320,"wires":[["9329ce50b06d5bd4"]]},{"id":"40d03418faeccfc1","type":"debug","z":"a3c75a60826c7302","name":"Failed","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1250,"y":300,"wires":[]},{"id":"9329ce50b06d5bd4","type":"switch","z":"a3c75a60826c7302","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":":false","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1070,"y":320,"wires":[["40d03418faeccfc1"],["35593195f92cb82a"]]},{"id":"35593195f92cb82a","type":"debug","z":"a3c75a60826c7302","name":"All OK","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1250,"y":340,"wires":[]}]

```

---

<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: [7 January 2024 12:55 UTC](https://discourse.nodered.org/t/neater-way-of-checking-ping-results/84343/8 "2024-01-07T12:55:10Z")

</div>

No need for JSON node  
Here is example using JSONata in switch node.

```auto
[{"id":"12131cadced372e8","type":"inject","z":"a3c75a60826c7302","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":320,"wires":[["af20663a3ba2595d"]]},{"id":"af20663a3ba2595d","type":"function","z":"a3c75a60826c7302","name":"function 1","func":"msg.payload = [\n {\n \"host\": \"google.com\",\n \"name\": \"external test point\",\n \"timeout\": 10000,\n },\n {\n \"host\": \"testsitetofail.com\",\n \"name\": \"external fail point\",\n \"timeout\": 10000,\n }\n]\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":320,"wires":[["1a871f9f3426c836"]]},{"id":"1a871f9f3426c836","type":"ping","z":"a3c75a60826c7302","protocol":"Automatic","mode":"triggered","name":"","host":"","timer":"20","inputs":1,"x":570,"y":320,"wires":[["f718fbc578ac622b"]]},{"id":"f718fbc578ac622b","type":"join","z":"a3c75a60826c7302","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":730,"y":320,"wires":[["74925663b4591d50","9329ce50b06d5bd4"]]},{"id":"74925663b4591d50","type":"debug","z":"a3c75a60826c7302","name":"Raw Payload","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":440,"wires":[]},{"id":"9329ce50b06d5bd4","type":"switch","z":"a3c75a60826c7302","name":"","property":"$not(false in $$.payload.*)","propertyType":"jsonata","rules":[{"t":"false"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1070,"y":320,"wires":[["40d03418faeccfc1"],["35593195f92cb82a"]]},{"id":"40d03418faeccfc1","type":"debug","z":"a3c75a60826c7302","name":"Failed","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1250,"y":300,"wires":[]},{"id":"35593195f92cb82a","type":"debug","z":"a3c75a60826c7302","name":"All OK","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1250,"y":340,"wires":[]}]

```

---

<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: [7 March 2024 12:56 UTC](https://discourse.nodered.org/t/neater-way-of-checking-ping-results/84343/9 "2024-03-07T12:56:05Z")

</div>

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