# Read data from JSON array

**URL:** https://discourse.nodered.org/t/read-data-from-json-array/1938
**Category:** Dashboard
**Created:** [31 July 2018 18:39 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938 "2018-07-31T18:39:06Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [31 July 2018 18:39 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/1 "2018-07-31T18:39:06Z")

</div>

I have function node to read data from JSON file, I want read-only last value in the array. I have written the following code in function node but shown error " "TypeError: Cannot read property 'length' of undefined"``

function node code is

var array = [];  
for (var i=0; i\<msg.payload.value.length;i++) {  
array.push({payload: msg.payload.value[i]});  
}

return array;  
the following output for json node that I want read last value of it by using the code above  
 ![pppppppppppppppppp](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/70c618871af45933a106f1df4838b33d3a2db420.PNG)

please, can anyone help me ???

---

<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: [31 July 2018 18:58 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/2 "2018-07-31T18:58:24Z")

</div>

That looks like a string not a json object. Maybe need a json node to convert first

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [31 July 2018 19:16 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/3 "2018-07-31T19:16:28Z")

</div>

Thank for your reply,  
I have tried json node but still the same error. I could not access to last value in the last object, please could you guide me ?  
after covert it to josn object still the same problem as shown below

![7777fff](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/650d5e6679b47c1ebac6f7ea43bd922c352cde33.PNG)

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [31 July 2018 19:25 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/4 "2018-07-31T19:25:14Z")

</div>

Replace the existing code from your function by this single line and check the result.

```auto
return {"payload" : msg.payload.pop()};

```

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [31 July 2018 19:37 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/5 "2018-07-31T19:37:15Z")

</div>

Thanks, Sir for your nice reply, it returns the last document but what I want exactly serach through all document and display only the document that has the current date (based on timestamp), please cloud you help me with !!

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [31 July 2018 20:10 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/6 "2018-07-31T20:10:41Z")

</div>

It could be done with a simple iteration but it is a little bit difficult since I do not have a dataset for testing.

Try to use this code inside a function node and please show me the result that will appear in your debug panel. This will tell me I got right the structure of your dataset.

```auto
function extract(elem) {
    let extr = elem.payload.timestamp;
    node.warn(extr)
    return extr;
    }
    
msg.payload.forEach(extract);

return msg;

```

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [31 July 2018 20:18 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/7 "2018-07-31T20:18:24Z")

</div>

this the output  
 ![9999999999999999999999](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/594f5bb12b87e9392c3ad28aef2201244d76b3d0.PNG)

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [31 July 2018 20:57 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/8 "2018-07-31T20:57:23Z")

</div>

I am not optimistic that this code will work since I could not test thoroughly and looks like your payload is not consistent (weird data inside the timestamp value) but lets give a try...

Try this code inside a function node

```auto
let today = new Date().toDateString();

let extractor = function (elem) {
    let extr = elem.payload.timestamp.toDateString();
    node.warn(extr);
    return extr == today;
}
    
var output = msg.payload.filter(extractor);
node.warn(output);

return msg;

```

Explanation:

it creates a function named extractor that will return true if the timestamp in your array element equals today. Otherwise the function returns false. Every time this function returns "true" the array element will be added to the array "output"

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [31 July 2018 21:19 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/9 "2018-07-31T21:19:51Z")

</div>

Thanks so much for your support, I have tried it and given this an ![eeeeeeeeeeeeeeeeeee](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/aac7910e0713fb015eae87e773bb5b386d7c3d6c.PNG) error

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [31 July 2018 21:32 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/10 "2018-07-31T21:32:43Z")

</div>

I was expecting this error. I could only help now if you export your flow or at least the array you are trying to work out. If you share your flow don´t forget to add 3 backticks before and after otherwise it wont be possible to import and test it.

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [31 July 2018 21:42 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/11 "2018-07-31T21:42:54Z")

</div>

is that clear, all objects have the same data but with different times as shown below.

![bbbbbbbbbbbbbbbbbbb](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7afab81e47a614cee9757a40e974d436ad473706.PNG)

this flow read from DB then convert the data to JSON Object .after that the main job for function node is read-only the document that has date equal to the current date.

please if something not clear let me know

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [31 July 2018 21:45 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/12 "2018-07-31T21:45:01Z")

</div>

This flow read from DB then convert the data to JSON Object .After that the main job for function node is reading -only the documents that have the date equal to the current date.

 ![ffffffffffffffff](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/deb8ec425641ac0e3ab564e984714dba8c29f1be.PNG)

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [31 July 2018 22:01 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/13 "2018-07-31T22:01:49Z")

</div>

The issue is that the timestamp is stored in different formats. I don´t know how to overcome this issue.

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [31 July 2018 22:26 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/14 "2018-07-31T22:26:32Z")

</div>

One last attempt ?

Check if this code works inside the function node, without generating errors.

```auto
let today = new Date().toDateString();

let extractor = function (elem) {
    let extr = new Date(elem.payload.timestamp).toDateString();
    node.warn(extr);
    return extr == today;
}
    
var output = msg.payload.filter(extractor);
node.warn(output);

return msg;

```

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [31 July 2018 22:50 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/15 "2018-07-31T22:50:47Z")

</div>

Yes ,it works without any errors

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [31 July 2018 23:06 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/16 "2018-07-31T23:06:57Z")

</div>

Could you please post a screenshot of the debug panel ? Just wanted to see how the output looks like.

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [31 July 2018 23:09 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/17 "2018-07-31T23:09:30Z")

</div>

yes,sure

 ![ttttttttttttttttttttttttttttttttttttttt](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2ccd505a484cec7eb7a1eaabbabdd4fe7e9687fc.PNG)

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [1 August 2018 08:16 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/18 "2018-08-01T08:16:09Z")

</div>

The code works without error but why showing all documents??????

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [1 August 2018 10:39 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/19 "2018-08-01T10:39:17Z")

</div>

The code provided will only select elements of the array and display them in the debug node in yellow color (thanks to the node.warn statement). Now it is up to you to define what properties of the object you want to display. If all properties then modify the code by adding this line at the end

```auto
msg.payload = output;

```

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [1 August 2018 10:42 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/20 "2018-08-01T10:42:05Z")

</div>

Ok ,I will try that then I will let you know.Thank,for every thanks

[Next page](https://discourse.nodered.org/t/read-data-from-json-array/1938.md?page=2)
