# Extracting object name dynamically

**URL:** <https://discourse.nodered.org/t/extracting-object-name-dynamically/52894>\
**Category:** General\
**Created:** [28 October 2021 15:46 UTC](https://discourse.nodered.org/t/extracting-object-name-dynamically/52894 "2021-10-28T15:46:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PhP60](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@PhP60](https://discourse.nodered.org/u/PhP60)\
**Post date:** [28 October 2021 15:46 UTC](https://discourse.nodered.org/t/extracting-object-name-dynamically/52894/1 "2021-10-28T15:46:52Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/3/13e24dcb82b3bd3b19f2707ad86858ff755e5a85.png)

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/8/b85a42a1dc0b4d62311422653be57fd76bdc59f6.png)

```auto
[{"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.

---

<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:** [28 October 2021 16:02 UTC](https://discourse.nodered.org/t/extracting-object-name-dynamically/52894/2 "2021-10-28T16:02:56Z")

</div>

You can access the key names using `Object.entries()`. See [Object.entries() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries)

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).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [11 November 2021 16:02 UTC](https://discourse.nodered.org/t/extracting-object-name-dynamically/52894/3 "2021-11-11T16:02:57Z")

</div>

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