Read data from JSON array


#1

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


#2

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


#3

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


#4

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

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

#5

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


#6

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;

#7

this the output
9999999999999999999999


#8

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"


#9

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


#10

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.


#11

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


#12

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.


#13

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


#14

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;

#15

Yes ,it works without any errors


#16

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


#17

yes,sure


#18

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


#19

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;

#20

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