Extracting object name dynamically

Hi there, (please, be cool, I'm a beginner),

I programmed an API by using a http node though which one I gather a structure "as JSON" format in the body.
These data contain two fields: deviceId and an object (including many rows) which this one could have different name depending kind of data.
I'm able to JSONify this one to use/parse it in a JSON.
image

My question is, as the name of the object (here reppercTEMPdegC) can be change following API request, how is it possible to extract this name?

Goal is, depending name, I would like to check number of row inside.

Example of other object name: reppercHUMIDITY_PERC, usageWATER_LIT, etc

[{"id":"34205fdafc79431e","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"16172618c78ebcae","type":"http response","z":"34205fdafc79431e","name":"","statusCode":"200","headers":{"content-type":"application/json","Access-Control-Allow-Methods":"POST","Access-Control-Allow-Origin":"*"},"x":1140,"y":200,"wires":[]},{"id":"45ca6cadc4b24ce4","type":"http in","z":"34205fdafc79431e","name":"","url":"/data","method":"post","upload":false,"swaggerDoc":"","x":120,"y":220,"wires":[["91a0b2494ca5e24f"]]},{"id":"91a0b2494ca5e24f","type":"function","z":"34205fdafc79431e","name":"JSON split","func":"var newMsg = {payload : JSON.parse(msg.req.body) };\n\nnewMsg.req  = msg.req;\nnewMsg.res = msg.res;\n\nreturn newMsg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":170,"y":320,"wires":[["9c5f43b661e30175"]]},{"id":"9c5f43b661e30175","type":"switch","z":"34205fdafc79431e","name":"Check DeviceId","property":"payload.deviceId","propertyType":"msg","rules":[{"t":"nnull"},{"t":"null"}],"checkall":"true","repair":false,"outputs":2,"x":400,"y":320,"wires":[["723fa468e0347008"],["53ac3a1907bcb6fa"]]},{"id":"53ac3a1907bcb6fa","type":"template","z":"34205fdafc79431e","name":"Response NOK - DeviceId missing","field":"payload","fieldType":"msg","format":"json","syntax":"plain","template":"[\n    {\n        \"response\" : \"Missing DeviceId in the JSON body\"\n    }\n]","output":"json","x":740,"y":460,"wires":[["773032d3d3367f3b"]]},{"id":"d6f663c526ae0d7a","type":"template","z":"34205fdafc79431e","name":"Response OK","field":"payload","fieldType":"msg","format":"json","syntax":"plain","template":"[\n    {\n        \"response\" : \"OK\"\n    }\n]","output":"json","x":920,"y":200,"wires":[["16172618c78ebcae"]]},{"id":"773032d3d3367f3b","type":"http response","z":"34205fdafc79431e","name":"","statusCode":"400","headers":{"content-type":"application/json","Access-Control-Allow-Methods":"POST","Access-Control-Allow-Origin":"*"},"x":1140,"y":480,"wires":[]},{"id":"723fa468e0347008","type":"switch","z":"34205fdafc79431e","name":"Check DeviceId content","property":"payload.deviceId","propertyType":"msg","rules":[{"t":"nempty"},{"t":"empty"}],"checkall":"true","repair":false,"outputs":2,"x":650,"y":300,"wires":[["d6f663c526ae0d7a","dc2178baa7166fb4"],["c553ba154d899429"]]},{"id":"c553ba154d899429","type":"template","z":"34205fdafc79431e","name":"Response NOK - DeviceId void","field":"payload","fieldType":"msg","format":"json","syntax":"plain","template":"[\n    {\n        \"response\" : \"Empty DeviceId in the JSON body\"\n    }\n]","output":"json","x":730,"y":420,"wires":[["773032d3d3367f3b"]]},{"id":"dc2178baa7166fb4","type":"debug","z":"34205fdafc79431e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1130,"y":280,"wires":[]}]

Many thanks for your help !
Have a nice day.

You can access the key names using Object.entries(). See Object.entries() - JavaScript | MDN

However, since you have programmed the api, it might be better to ammend the API so it returns the data as a fixed name object, with a type field that identifies the name. Or perhaps have different API methods to get the different types explicitly (if that is more appropriate).

1 Like

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