# 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:** 2

<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:** [2 August 2018 12:04 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/21 "2018-08-02T12:04:40Z")

</div>

Hi Andrei,

I have used this code, to retrieve the last document based on timestamp from cloudant db. For example, if I have two documents first one with 2/8/2018 12:3 and second document with 2/8/2018 12: 7- timestamp. I expect this code to retrieve the second document with 2/8/2018 12:7 only and ignore the rest.

The code in the function node

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);  
msg.payload = output;  
return output;

The result is :  
 ![resu](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e577b7d6b1d2bc5e5db897057fbb94564d591fad.PNG)

The actual data that I have :

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

 ![yyyy](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/5e74f86397263ac5907d6baa7ef0ad8bc72ce311.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:** [2 August 2018 12:08 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/22 "2018-08-02T12:08:18Z")

</div>

Please guide me to solve this problem 😥

---

<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:** [2 August 2018 13:49 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/23 "2018-08-02T13:49:56Z")

</div>

Sir,

I just want to point out that the code now works correctly but the problem is when finding two documents in the same date, for example, the first document with 2/8/2018 2:20 and second documents with date 2/8/2018 2: 30, it will retrieve the first documents (2/8/2018 2:20 ). How can make it read the last document with date and time, please?  
as shown below ,I have six objects all of them the same date but in different  
times,the code selected only the object 0 while it should select last one .  
 ![11155555](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c65565359859b9f62a342ef4d9eed9efc8de57c3.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:** [2 August 2018 15:00 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/24 "2018-08-02T15:00:50Z")

</div>

> [@Ahd](#):
>
> How can make it read the last document with date and time, please?

Good, the answer for this question is using the pop() method (extract the last element from an array).

The code in your function should looks like below:

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

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

return msg;

```

You will see the last element in yelllow color in the debug panel and it will be sent in msg.payload (add a debug node after your function to inspect msg payload).

---

<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:** [2 August 2018 15:24 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/25 "2018-08-02T15:24:32Z")

</div>

Thanks for your reply,  
The code given me the following errors

![er](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/48d2044c465dd19af2bbc8dddc19ff06aed431a7.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:** [2 August 2018 15:33 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/26 "2018-08-02T15:33:55Z")

</div>

Indeed: I have posted wrongly. Correction below:

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

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

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:** [2 August 2018 15:42 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/27 "2018-08-02T15:42:30Z")

</div>

I am confusing about something, I will be greatfull if anyone can expain it to me.

When my function node inserted the data into cloudant db, I have noticed that the documents stored not in order. For instance,I was thinking that my documents organized as following the first insert will then first in the db and the second will be second and so on, as shown below

 ![ex](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/97db09837c1fcef8518d11d45c7422043568b0fe.PNG)

But what I have found that when checked the document content and timestime that stored as followed

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

what is the wrong with this please !!

---

<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:** [2 August 2018 15:46 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/28 "2018-08-02T15:46:59Z")

</div>

I don´t know if it will be possible for someone to reply about the writing order in the database without having a look in your whole flow.

How about the question asked before (retrieve the last element) ? Is it all right now ?

---

<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:** [2 August 2018 15:55 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/29 "2018-08-02T15:55:55Z")

</div>

NO, sir, I still have the same issue due to db not in order. The codes going to selected first the document with current date only from db but in case there more than one documents in the same date but a different time then the code looking only for the first document with the current time and ignore that latest document with current time .

Sir, I am ready to share my desktop with you to show you exact problem if you don't mind, please.

---

<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:** [2 August 2018 15:59 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/30 "2018-08-02T15:59:30Z")

</div>

I don't know about cloudant but generally with a database you should always use a sort\_by specification when retrieving data. So in your case you would sort by the date/time ascending, then select the last one, or descending and select the first. In SQL you can sort by a field and then use LIMIT to say how many you want so you would sort by date/time descending with LIMIT 1 and you would get the latest one.

---

<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:** [2 August 2018 16:12 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/31 "2018-08-02T16:12:39Z")

</div>

I guess the best approach is like @Colin said (retrieving the data in the proper order). However it should not be difficult to change the function code to return the newest element (based on the timestamp). The code was written this way as your original statement was to retrieve the last in order (not the newest) element.

---

<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:** [2 August 2018 16:20 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/32 "2018-08-02T16:20:52Z")

</div>

Thank you @Andrei  
Thank you @Colin

I will consider that ,thanks again for your help .

---

<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:** [2 August 2018 17:00 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/33 "2018-08-02T17:00:24Z")

</div>

Hi @Ahd,

This would be the modified function node. Again, I do not have a dataset for testing so there is always the risk that something is not good.

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

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

let output2 = output1.sort(function (a, b) {
    let atime = new Date(a.payload.timestamp);
    let btime = new Date(b.payload.timestamp);
  return atime-btime;
});

let last = output2.pop();

node.warn(last);
msg.payload =last;

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:** [2 August 2018 23:29 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/34 "2018-08-02T23:29:44Z")

</div>

Sir,  
I have run it then, it showed the following result

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

![nnn](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/34807033f1be355b7e45a9870fb8905719c694fb.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:** [3 August 2018 01:27 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/35 "2018-08-03T01:27:00Z")

</div>

mmm... the function assumes that the timestamps value is a property nested inside the payload, like below:

![r-001](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c0a947a8cd2657f7445d4fb936e5299a85b24b31.PNG)

Your post shows timestamp inside a different structure, like below:

![r-002](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/8eb6db41150ebbe2af85e75f1d5fd2ea7fcc7087.PNG)

Can you post another screenshot of the debug that shows how the timestamp appears inside the object?

The code, as written now, will first select the objects that have timestamp equals today (it will be a problem if there is no object stored with todaýs date) generating a short array (variable output1) . This array will then be sorted out based on the timestamp and stored into the variable output2 . This last element (supposedly the newest one) is then extracted to the variable named "last" and stored in msg.payload.

---

<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:** [3 August 2018 06:12 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/36 "2018-08-03T06:12:28Z")

</div>

You really would be much better to use the features of the database to give you just the latest. That is what databases are designed to do efficiently. To read them all in and then sort them is not the way to do 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:** [3 August 2018 08:41 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/37 "2018-08-03T08:41:05Z")

</div>

Hi @Andrei,

The screen below shows exact data that I have in DB.  
 ![bbbbbbbbbbbbbbbbbbbbbbb](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/bb7d665bd435f42e603a2b8e9325a203fed32ad0.PNG)

By the way, when tested the code on the following data, the result was correct,  
 ![nnnnnnnnnnnnnnnnnnnn](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/87be2232d51438ab085cd0343b49312cd946cb4b.PNG)

But it fails when I have tested it on the real data that I have in my project which is shown in full screen above . Each object has two topics of data first is data which include time stamp and second is an image which includes image information.

---

<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:** [3 August 2018 08:46 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/38 "2018-08-03T08:46:57Z")

</div>

Thanks@ Colin

I have tried my best to do that but I found cloudant db much different than sqldb. Because of it store the documents, not in order. the second stored them in object form and nest array that make the read more difficult.

---

<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:** [3 August 2018 09:59 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/39 "2018-08-03T09:59:20Z")

</div>

Maybe you are not storing them the best way then. I cannot believe that it is not possible to attach a date to a document and then sort by date, that is such a basic requirement.  
Like this possibly

> <https://stackoverflow.com/questions/33833898/getting-the-latest-timestamp-wise-value-from-cloudant-query>

---

<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:** [3 August 2018 11:19 UTC](https://discourse.nodered.org/t/read-data-from-json-array/1938/40 "2018-08-03T11:19:24Z")

</div>

> [@Ahd](#):
>
> Each object has two topics of data first is data which include time stamp and second is an image which includes image information.

That explains the issue. In such case test this code inside the function node. It will fetch the timestamp inside payload.data

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

let extractor = function (elem) {
    let extr = new Date(elem.payload.data.timestamp).toDateString();
    return extr == today;
}
    
let output1 = msg.payload.filter(extractor);

let output2 = output1.sort(function (a, b) {
    let atime = new Date(a.payload.data.timestamp);
    let btime = new Date(b.payload.data.timestamp);
  return atime-btime;
});

let last = output2.pop();

node.warn(last);
msg.payload =last;

return msg;

```

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

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