# Changing String to JavaScript Object

**URL:** https://discourse.nodered.org/t/changing-string-to-javascript-object/80078
**Category:** General
**Tags:** mqtt
**Created:** [26 July 2023 09:47 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078 "2023-07-26T09:47:56Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![Lorenzo953](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@Lorenzo953](https://discourse.nodered.org/u/Lorenzo953)
#### Post date: [26 July 2023 09:47 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/1 "2023-07-26T09:47:56Z")

</div>

I'm trying to readout a heat meter. The data of the meter is send via M-Bus to a Teltonika TRB 143 router and then send over MQTT to my NodeRed. On the router i can choose between the data format XML and HEX.  
if i use XML i get the following data:

```auto
<?xml version="1.0" encoding="ISO-8859-1"?> <MBusData> <SlaveInformation> <Id>66180143</Id> <Manufacturer>DME</Manufacturer> <Version>160</Version> <ProductName></ProductName> 
<Medium>Heat: Outlet</Medium> <AccessNumber>0</AccessNumber> <Status>00</Status> <Signature>0000</Signature> </SlaveInformation> 
<DataRecord id="0"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Energy (10 kWh)</Unit> <Value>70880</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>
</DataRecord> <DataRecord id="1"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Tariff>1</Tariff> <Device>0</Device> <Unit>Energy (10 kWh)</Unit> <Value>0</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>
 </DataRecord> <DataRecord id="2"><Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Tariff>2</Tariff> <Device>0</Device> <Unit>Volume (1e-2 m^3)</Unit> <Value>31850</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>
 </DataRecord> <DataRecord id="3"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Volume (1e-2 m^3)</Unit> <Value>3297988</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>
 </DataRecord> <DataRecord id="4"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Power (W)</Unit> <Value>0</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>
 </DataRecord> <DataRecord id="5"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Volume flow (m m^3/h)</Unit> <Value>0</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>
 </DataRecord> <DataRecord id="6"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Flow temperature (1e-1 deg C)</Unit> <Value>752</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> 
</DataRecord> <DataRecord id="7"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Return temperature (1e-1 deg C)</Unit> <Value>715</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> 
</DataRecord> <DataRecord id="8"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Temperature Difference (1e-1 deg C)</Unit> <Value>37</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> 
</DataRecord> <DataRecord id="9"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Operating time (hours)</Unit> <Value>30728</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> 
</DataRecord> <DataRecord id="10"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Time Point (time &amp; date)</Unit> <Value>2023-07-25T16:23:00</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> 
</DataRecord> <DataRecord id="11"> <Function>Instantaneous value</Function> <StorageNumber>1</StorageNumber> <Unit>Energy (10 kWh)</Unit> <Value>70579</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>
 </DataRecord> <DataRecord id="12"> <Function>Instantaneous value</Function> <StorageNumber>1</StorageNumber> <Unit>Volume (1e-2 m^3)</Unit> <Value>3280495</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>
 </DataRecord> <DataRecord id="13"> <Function>Instantaneous value</Function> <StorageNumber>1</StorageNumber> <Tariff>1</Tariff> <Device>0</Device> <Unit>Energy (10 kWh)</Unit> <Value>0</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>
 </DataRecord> <DataRecord id="14"> <Function>Instantaneous value</Function> <StorageNumber>1</StorageNumber> <Tariff>2</Tariff> <Device>0</Device> <Unit>Volume (1e-2 m^3)</Unit> <Value>31846</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>
 </DataRecord> <DataRecord id="15"> <Function>Instantaneous value</Function> <StorageNumber>1</StorageNumber> <Unit>Time Point (date)</Unit> <Value>2023-06-30</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> </DataRecord> </MBusData> `

```

With the MQTT Node i only get a string as an output and cannot use the data, i want a JavaScript Object. When i choose the option: "output: parsed J-SON object", i just get "failed to parse json string".

How can i convert my string to a JS object?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [26 July 2023 09:50 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/2 "2023-07-26T09:50:37Z")

</div>

Looks like an xml string, have you tried passing it through the xml node? Then you could possibly parse the string values.  
[edit] with new xml shown this will work.

---

<div class="post-metadata">

### Author: ![Lorenzo953](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@Lorenzo953](https://discourse.nodered.org/u/Lorenzo953)
#### Post date: [26 July 2023 09:59 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/3 "2023-07-26T09:59:14Z")

</div>

So the Problem is with the meter?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [26 July 2023 10:27 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/4 "2023-07-26T10:27:23Z")

</div>

The problem is the xml is not well formed, you could still parse the text  
this will return an array of arrays, each line has been split into an array.

```auto
[{"id":"fb225a074e30c5e4","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":2580,"wires":[["0355d5d7d41485f9"]]},{"id":"0355d5d7d41485f9","type":"template","z":"b9860b4b9de8c8da","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> 66180143 DME 160\nHeat: Outlet 0 00 0000\nInstantaneous value 0 Energy (10 kWh) 70880 2023-07-25T13:54:03Z\nInstantaneous value 0 1 0 Energy (10 kWh) 0 2023-07-25T13:54:03Z\nInstantaneous value 0 2 0 Volume (1e-2 m^3) 31850 2023-07-25T13:54:03Z\nInstantaneous value 0 Volume (1e-2 m^3) 3297988 2023-07-25T13:54:03Z\nInstantaneous value 0 Power (W) 0 2023-07-25T13:54:03Z\nInstantaneous value 0 Volume flow (m m^3/h) 0 2023-07-25T13:54:03Z\nInstantaneous value 0 Flow temperature (1e-1 deg C) 752 2023-07-25T13:54:03Z\nInstantaneous value 0 Return temperature (1e-1 deg C) 715 2023-07-25T13:54:03Z\nInstantaneous value 0 Temperature Difference (1e-1 deg C) 37 2023-07-25T13:54:03Z\nInstantaneous value 0 Operating time (hours) 30728 2023-07-25T13:54:03Z\nInstantaneous value 0 Time Point (time & date) 2023-07-25T16:23:00 2023-07-25T13:54:03Z\nInstantaneous value 1 Energy (10 kWh) 70579 2023-07-25T13:54:03Z\nInstantaneous value 1 Volume (1e-2 m^3) 3280495 2023-07-25T13:54:03Z\nInstantaneous value 1 1 0 Energy (10 kWh) 0 2023-07-25T13:54:03Z\nInstantaneous value 1 2 0 Volume (1e-2 m^3) 31846 2023-07-25T13:54:03Z\nInstantaneous value 1 Time Point (date) 2023-06-30 2023-07-25T13:54:03Z","output":"str","x":260,"y":2580,"wires":[["aba5d542df68bb74"]]},{"id":"aba5d542df68bb74","type":"split","z":"b9860b4b9de8c8da","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":390,"y":2580,"wires":[["88f3458b8494196d"]]},{"id":"88f3458b8494196d","type":"split","z":"b9860b4b9de8c8da","name":"","splt":" ","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":470,"y":2620,"wires":[["f50ca5f0b41836f4"]]},{"id":"f50ca5f0b41836f4","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"parts.type","pt":"msg","to":"array","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":2620,"wires":[["27c123f9bffb9a28"]]},{"id":"27c123f9bffb9a28","type":"join","z":"b9860b4b9de8c8da","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":790,"y":2620,"wires":[["dad8f729924fec2a"]]},{"id":"dad8f729924fec2a","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"parts.type","pt":"msg","to":"array","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":2580,"wires":[["75f30a6a2953f462"]]},{"id":"75f30a6a2953f462","type":"join","z":"b9860b4b9de8c8da","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"parts.key","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"15","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":950,"y":2580,"wires":[["f7d9c9c5f5dcd2a0"]]},{"id":"f7d9c9c5f5dcd2a0","type":"debug","z":"b9860b4b9de8c8da","name":"debug 324","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1090,"y":2580,"wires":[]}]

```

[edit] with the supplied xml data you could do this

```auto
[{"id":"d2d53d98561094b0","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":2400,"wires":[["f939ec7239531a3d"]]},{"id":"f939ec7239531a3d","type":"template","z":"b9860b4b9de8c8da","name":"simulte incoming","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> <MBusData> <SlaveInformation> <Id>66180143</Id> <Manufacturer>DME</Manufacturer> <Version>160</Version> <ProductName></ProductName> \n<Medium>Heat: Outlet</Medium> <AccessNumber>0</AccessNumber> <Status>00</Status> <Signature>0000</Signature> </SlaveInformation> \n<DataRecord id=\"0\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Energy (10 kWh)</Unit> <Value>70880</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n</DataRecord> <DataRecord id=\"1\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Tariff>1</Tariff> <Device>0</Device> <Unit>Energy (10 kWh)</Unit> <Value>0</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"2\"><Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Tariff>2</Tariff> <Device>0</Device> <Unit>Volume (1e-2 m^3)</Unit> <Value>31850</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"3\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Volume (1e-2 m^3)</Unit> <Value>3297988</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"4\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Power (W)</Unit> <Value>0</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"5\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Volume flow (m m^3/h)</Unit> <Value>0</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"6\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Flow temperature (1e-1 deg C)</Unit> <Value>752</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> \n</DataRecord> <DataRecord id=\"7\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Return temperature (1e-1 deg C)</Unit> <Value>715</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> \n</DataRecord> <DataRecord id=\"8\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Temperature Difference (1e-1 deg C)</Unit> <Value>37</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> \n</DataRecord> <DataRecord id=\"9\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Operating time (hours)</Unit> <Value>30728</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> \n</DataRecord> <DataRecord id=\"10\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Time Point (time &amp; date)</Unit> <Value>2023-07-25T16:23:00</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> \n</DataRecord> <DataRecord id=\"11\"> <Function>Instantaneous value</Function> <StorageNumber>1</StorageNumber> <Unit>Energy (10 kWh)</Unit> <Value>70579</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"12\"> <Function>Instantaneous value</Function> <StorageNumber>1</StorageNumber> <Unit>Volume (1e-2 m^3)</Unit> <Value>3280495</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"13\"> <Function>Instantaneous value</Function> <StorageNumber>1</StorageNumber> <Tariff>1</Tariff> <Device>0</Device> <Unit>Energy (10 kWh)</Unit> <Value>0</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"14\"> <Function>Instantaneous value</Function> <StorageNumber>1</StorageNumber> <Tariff>2</Tariff> <Device>0</Device> <Unit>Volume (1e-2 m^3)</Unit> <Value>31846</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"15\"> <Function>Instantaneous value</Function> <StorageNumber>1</StorageNumber> <Unit>Time Point (date)</Unit> <Value>2023-06-30</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> </DataRecord> </MBusData> ","output":"str","x":330,"y":2400,"wires":[["4956b07907aa9190"]]},{"id":"4956b07907aa9190","type":"xml","z":"b9860b4b9de8c8da","name":"","property":"payload","attr":"","chr":"","x":450,"y":2420,"wires":[["6bbb9759d80e52fd"]]},{"id":"6bbb9759d80e52fd","type":"debug","z":"b9860b4b9de8c8da","name":"debug 325","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":2440,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![Lorenzo953](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@Lorenzo953](https://discourse.nodered.org/u/Lorenzo953)
#### Post date: [26 July 2023 13:34 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/5 "2023-07-26T13:34:16Z")

</div>

the second flow looks good, but when i try it with the original message i get : "Error: Non-whitespace before first tag.  
Line: 0  
Column: 1  
Char: ["

---

<div class="post-metadata">

### Author: ![Lorenzo953](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@Lorenzo953](https://discourse.nodered.org/u/Lorenzo953)
#### Post date: [26 July 2023 13:35 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/6 "2023-07-26T13:35:22Z")

</div>

This is what my MQTT Node recieves:

```auto
[<?xml version="1.0" encoding="ISO-8859-1"?>
<MBusData>

    <SlaveInformation>
        <Id>66180143</Id>
        <Manufacturer>DME</Manufacturer>
        <Version>160</Version>
        <ProductName></ProductName>
        <Medium>Heat: Outlet</Medium>
        <AccessNumber>144</AccessNumber>
        <Status>00</Status>
        <Signature>0000</Signature>
    </SlaveInformation>

    <DataRecord id="0">
        <Function>Instantaneous value</Function>
        <StorageNumber>0</StorageNumber>
        <Unit>Energy (10 kWh)</Unit>
        <Value>70892</Value>
        <Timestamp>2023-07-26T13:33:21Z</Timestamp>
    </DataRecord>

    <DataRecord id="1">
        <Function>Instantaneous value</Function>
        <StorageNumber>0</StorageNumber>
        <Tariff>1</Tariff>
        <Device>0</Device>
        <Unit>Energy (10 kWh)</Unit>
        <Value>0</Value>
        <Timestamp>2023-07-26T13:33:21Z</Timestamp>
    </DataRecord>

    <DataRecord id="2">
        <Function>Instantaneous value</Fun... 

```

Occurs the error because of the opening Gap?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [26 July 2023 14:12 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/7 "2023-07-26T14:12:25Z")

</div>

Where did the `[` come from in the first character? Not there in post 1 example.

---

<div class="post-metadata">

### Author: ![Lorenzo953](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@Lorenzo953](https://discourse.nodered.org/u/Lorenzo953)
#### Post date: [26 July 2023 14:14 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/8 "2023-07-26T14:14:17Z")

</div>

I accidentally posted the data from the router directly, not from the recieving MQTT node. Also the data seems to be cut

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [26 July 2023 14:31 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/9 "2023-07-26T14:31:01Z")

</div>

Show us a debug of the data coming in from mqtt node.  
The debug truncates display to 1000 chars, the data is still all there in the flow though.

---

<div class="post-metadata">

### Author: ![Lorenzo953](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@Lorenzo953](https://discourse.nodered.org/u/Lorenzo953)
#### Post date: [26 July 2023 14:41 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/10 "2023-07-26T14:41:06Z")

</div>

```auto
[,<?xml version="1.0" encoding="ISO-8859-1"?>
<MBusData>

    <SlaveInformation>
        <Id>66180143</Id>
        <Manufacturer>DME</Manufacturer>
        <Version>160</Version>
        <ProductName></ProductName>
        <Medium>Heat: Outlet</Medium>
        <AccessNumber>202</AccessNumber>
        <Status>00</Status>
        <Signature>0000</Signature>
    </SlaveInformation>

    <DataRecord id="0">
        <Function>Instantaneous value</Function>
        <StorageNumber>0</StorageNumber>
        <Unit>Energy (10 kWh)</Unit>
        <Value>70892</Value>
        <Timestamp>2023-07-26T14:29:46Z</Timestamp>
    </DataRecord>

    <DataRecord id="1">
        <Function>Instantaneous value</Function>
        <StorageNumber>0</StorageNumber>
        <Tariff>1</Tariff>
        <Device>0</Device>
        <Unit>Energy (10 kWh)</Unit>
        <Value>0</Value>
        <Timestamp>2023-07-26T14:29:46Z</Timestamp>
    </DataRecord>

    <DataRecord id="2">
        <Function>Instantaneous value</Fu...

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [26 July 2023 14:44 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/11 "2023-07-26T14:44:22Z")

</div>

I want to see the image/screenshot so i can see what type the info is, you have already posted the above and this time there is an extra `[,`. The image will clarify what is there.

---

<div class="post-metadata">

### Author: ![Lorenzo953](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@Lorenzo953](https://discourse.nodered.org/u/Lorenzo953)
#### Post date: [26 July 2023 14:53 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/12 "2023-07-26T14:53:55Z")

</div>

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

---

<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: [26 July 2023 14:58 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/13 "2023-07-26T14:58:09Z")

</div>

Is that coming straight out of the mqtt node? The topic looks odd, or is the mqtt topic `Test123`?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [26 July 2023 15:19 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/14 "2023-07-26T15:19:14Z")

</div>

Ok if that is coming from the mqtt node the data being sent is malformed. The characters before the \<xml .... \> need to be removed.

Either fix it in the sensor sending (if you have access) or you can use a change node to remove them  
e.g.

```auto
[{"id":"d2d53d98561094b0","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":2340,"wires":[["f939ec7239531a3d"]]},{"id":"f939ec7239531a3d","type":"template","z":"b9860b4b9de8c8da","name":"simulte incoming","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"[,<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> <MBusData> <SlaveInformation> <Id>66180143</Id> <Manufacturer>DME</Manufacturer> <Version>160</Version> <ProductName></ProductName> \n<Medium>Heat: Outlet</Medium> <AccessNumber>0</AccessNumber> <Status>00</Status> <Signature>0000</Signature> </SlaveInformation> \n<DataRecord id=\"0\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Energy (10 kWh)</Unit> <Value>70880</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n</DataRecord> <DataRecord id=\"1\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Tariff>1</Tariff> <Device>0</Device> <Unit>Energy (10 kWh)</Unit> <Value>0</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"2\"><Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Tariff>2</Tariff> <Device>0</Device> <Unit>Volume (1e-2 m^3)</Unit> <Value>31850</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"3\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Volume (1e-2 m^3)</Unit> <Value>3297988</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"4\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Power (W)</Unit> <Value>0</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"5\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Volume flow (m m^3/h)</Unit> <Value>0</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"6\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Flow temperature (1e-1 deg C)</Unit> <Value>752</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> \n</DataRecord> <DataRecord id=\"7\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Return temperature (1e-1 deg C)</Unit> <Value>715</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> \n</DataRecord> <DataRecord id=\"8\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Temperature Difference (1e-1 deg C)</Unit> <Value>37</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> \n</DataRecord> <DataRecord id=\"9\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Operating time (hours)</Unit> <Value>30728</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> \n</DataRecord> <DataRecord id=\"10\"> <Function>Instantaneous value</Function> <StorageNumber>0</StorageNumber> <Unit>Time Point (time &amp; date)</Unit> <Value>2023-07-25T16:23:00</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> \n</DataRecord> <DataRecord id=\"11\"> <Function>Instantaneous value</Function> <StorageNumber>1</StorageNumber> <Unit>Energy (10 kWh)</Unit> <Value>70579</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"12\"> <Function>Instantaneous value</Function> <StorageNumber>1</StorageNumber> <Unit>Volume (1e-2 m^3)</Unit> <Value>3280495</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"13\"> <Function>Instantaneous value</Function> <StorageNumber>1</StorageNumber> <Tariff>1</Tariff> <Device>0</Device> <Unit>Energy (10 kWh)</Unit> <Value>0</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"14\"> <Function>Instantaneous value</Function> <StorageNumber>1</StorageNumber> <Tariff>2</Tariff> <Device>0</Device> <Unit>Volume (1e-2 m^3)</Unit> <Value>31846</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp>\n </DataRecord> <DataRecord id=\"15\"> <Function>Instantaneous value</Function> <StorageNumber>1</StorageNumber> <Unit>Time Point (date)</Unit> <Value>2023-06-30</Value> <Timestamp>2023-07-25T13:54:03Z</Timestamp> </DataRecord> </MBusData> ","output":"str","x":350,"y":2340,"wires":[["fc42e1307ac4d436"]]},{"id":"fc42e1307ac4d436","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"<\" & $substringAfter($$.payload,\"<\") ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":2400,"wires":[["4956b07907aa9190"]]},{"id":"4956b07907aa9190","type":"xml","z":"b9860b4b9de8c8da","name":"","property":"payload","attr":"","chr":"","x":470,"y":2400,"wires":[["6bbb9759d80e52fd"]]},{"id":"6bbb9759d80e52fd","type":"debug","z":"b9860b4b9de8c8da","name":"debug 325","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":2380,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![Lorenzo953](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@Lorenzo953](https://discourse.nodered.org/u/Lorenzo953)
#### Post date: [27 July 2023 11:48 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/15 "2023-07-27T11:48:56Z")

</div>

i named the topic like test for testing purpose

---

<div class="post-metadata">

### Author: ![Lorenzo953](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@Lorenzo953](https://discourse.nodered.org/u/Lorenzo953)
#### Post date: [27 July 2023 11:53 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/16 "2023-07-27T11:53:59Z")

</div>

Then I get the error:  
error

Error: Unclosed root tag Line: 67 Column: 26 Char:

---

<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: [27 July 2023 12:45 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/17 "2023-07-27T12:45:36Z")

</div>

Set the debug node showing what is coming from mqtt to output to the console (as well as the debug pane). Then look in the node-red log and you will be able to see the complete object and check the end looks correct.

---

<div class="post-metadata">

### Author: ![Lorenzo953](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@Lorenzo953](https://discourse.nodered.org/u/Lorenzo953)
#### Post date: [27 July 2023 14:52 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/18 "2023-07-27T14:52:31Z")

</div>

It seems like it's just working, if the Router isn't sending the messages every minute. I changed it to 10 minutes, now it`s working. Thank you all very much!!!

---

<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: [10 August 2023 14:53 UTC](https://discourse.nodered.org/t/changing-string-to-javascript-object/80078/19 "2023-08-10T14:53:24Z")

</div>

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