Read data from JSON array

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

please, can anyone help me ???

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

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

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

return {"payload" : msg.payload.pop()};
1 Like

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 !!

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.

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

return msg;

this the output
9999999999999999999999

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

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"

Thanks so much for your support, I have tried it and given this an eeeeeeeeeeeeeeeeeee error

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.

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

bbbbbbbbbbbbbbbbbbb

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

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.

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

One last attempt ?

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

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;

Yes ,it works without any errors

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

1 Like

yes,sure

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

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

msg.payload = output;

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