# Trouble reading from JSObject created from XML

**URL:** <https://discourse.nodered.org/t/trouble-reading-from-jsobject-created-from-xml/75947>\
**Category:** General\
**Created:** [1 March 2023 15:36 UTC](https://discourse.nodered.org/t/trouble-reading-from-jsobject-created-from-xml/75947 "2023-03-01T15:36:43Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![Darren](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/darren/32/92202_2.png) [@Darren](https://discourse.nodered.org/u/Darren)\
**Post date:** [3 March 2023 15:40 UTC](https://discourse.nodered.org/t/trouble-reading-from-jsobject-created-from-xml/75947/9 "2023-03-03T15:40:13Z")

</div>

Turns out, there were some undocumented variables that I needed to account for that no-one informed me on. The corrected code:

```auto
let arr = msg.payload; // get the array
let rv = {}; // create a new empty object
let fc = []; 

arr.forEach(obj_payload => {
    if (obj_payload.variable) {
        obj_payload.variable.forEach(obj_variable => {
                let key = obj_variable.$.name;
                if (obj_variable.$.address) {

                    // assign function code depending on D or M (other) prefix
                    if (obj_variable.$.address.indexOf('D') > -1) {
                        fc = 1;
                    } else {
                        fc = 3;
                    }

                    // strip D/M from head of address
                    obj_variable.$.address = obj_variable.$.address.substring(1); 

                    // populate the rest of the JSObject
                    rv[key] = {
                        address: obj_variable.$.address, // addresses, trimmed of letters
                        array_data_type_name: "", // empty
                        fc: fc, // function code
                        quantity: 1, // empty
                        type_name: Object.keys(obj_variable.type[0]).toString(), //object keys
                        word_length: 1, // empty
                        unitid: 255 // previously added by lvc_labels
                    }; // replace arr[i] with selected pieces  
            }
        })
    }
});

msg.payload = rv;
return msg;

```

---

_[View the full topic](https://discourse.nodered.org/t/trouble-reading-from-jsobject-created-from-xml/75947)._
