# Parsing / Converting String to Object

**URL:** https://discourse.nodered.org/t/parsing-converting-string-to-object/70281
**Category:** General
**Created:** [8 November 2022 20:33 UTC](https://discourse.nodered.org/t/parsing-converting-string-to-object/70281 "2022-11-08T20:33:11Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![interscope101](https://avatars.discourse-cdn.com/v4/letter/i/a88e4f/32.png) [@interscope101](https://discourse.nodered.org/u/interscope101)
#### Post date: [8 November 2022 20:33 UTC](https://discourse.nodered.org/t/parsing-converting-string-to-object/70281/1 "2022-11-08T20:33:11Z")

</div>

Hi there, really need assistance parsing / converting this output (coming from exec-node). The best I was able to achieve using a string-node is to isolate a single variable. The output is always these 6 lines.

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

Or I can get simplified to

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/2/52d75e4061d35c8859c19a6575e630b3354dca75.png)

Trying to get an object in this format. Thanks in advance!

5m  
.. BUY: 6  
.. SELL: 10  
.. NEUTRAL: 10

15m  
.. BUY: 2  
.. SELL: 14  
.. NEUTRAL: 10

---

<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: [8 November 2022 21:18 UTC](https://discourse.nodered.org/t/parsing-converting-string-to-object/70281/2 "2022-11-08T21:18:45Z")

</div>

Can you return

```auto
[{"BUY": 7, "SELL": 10, "NEUTRAL": 9},
{"BUY": 7, "SELL": 10, "NEUTRAL": 9},
{"BUY": 7, "SELL": 10, "NEUTRAL": 9}]

```

As that would be a valid JSON string, which could be passed through a JSON node.  
Single quotes are not valid in a JSON

[edit] If not this should work on your initial output format

```auto
[{"id":"550f90e56d2a20f8","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":3240,"wires":[["4a1e21f38e83239f"]]},{"id":"4a1e21f38e83239f","type":"template","z":"452103ea51141731","name":"simulate exec node output","field":"payload","fieldType":"msg","format":"handlebars","syntax":"plain","template":"5m {'one':'buy', 'two': 1}\n15m {'one':'buy', 'two': 1}","output":"str","x":340,"y":3240,"wires":[["3444de9788e63442"]]},{"id":"3444de9788e63442","type":"split","z":"452103ea51141731","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":210,"y":3280,"wires":[["b8538daefdd37bf3"]]},{"id":"b8538daefdd37bf3","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"'","fromt":"str","to":"\"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":" {","fromt":"str","to":"\": {","tot":"str"},{"t":"set","p":"parts.ch","pt":"msg","to":", \"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":3280,"wires":[["881fc66a5dbb5c7e"]]},{"id":"881fc66a5dbb5c7e","type":"join","z":"452103ea51141731","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":510,"y":3280,"wires":[["80e95241cd91f75a"]]},{"id":"80e95241cd91f75a","type":"template","z":"452103ea51141731","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":" {\"{{{payload}}} }","output":"json","x":640,"y":3280,"wires":[["0d17775e4e4e5987"]]},{"id":"0d17775e4e4e5987","type":"debug","z":"452103ea51141731","name":"debug 106","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":490,"y":3340,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![interscope101](https://avatars.discourse-cdn.com/v4/letter/i/a88e4f/32.png) [@interscope101](https://discourse.nodered.org/u/interscope101)
#### Post date: [8 November 2022 21:29 UTC](https://discourse.nodered.org/t/parsing-converting-string-to-object/70281/3 "2022-11-08T21:29:09Z")

</div>

Using the node-string I can replace the single quotes and append brackets, but still need commas.

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

---

<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: [8 November 2022 21:31 UTC](https://discourse.nodered.org/t/parsing-converting-string-to-object/70281/4 "2022-11-08T21:31:44Z")

</div>

Can you post text, so i can edit.

You need commas were i put them at end of each line, apart from last.

---

<div class="post-metadata">

### Author: ![interscope101](https://avatars.discourse-cdn.com/v4/letter/i/a88e4f/32.png) [@interscope101](https://discourse.nodered.org/u/interscope101)
#### Post date: [8 November 2022 21:34 UTC](https://discourse.nodered.org/t/parsing-converting-string-to-object/70281/5 "2022-11-08T21:34:43Z")

</div>

This is how it's coming in raw

```auto
5m {'RECOMMENDATION': 'SELL', 'BUY': 2, 'SELL': 15, 'NEUTRAL': 9}
15m {'RECOMMENDATION': 'STRONG_SELL', 'BUY': 0, 'SELL': 16, 'NEUTRAL': 10}
30m {'RECOMMENDATION': 'SELL', 'BUY': 3, 'SELL': 14, 'NEUTRAL': 9}
1h {'RECOMMENDATION': 'SELL', 'BUY': 2, 'SELL': 15, 'NEUTRAL': 9}
2h {'RECOMMENDATION': 'STRONG_SELL', 'BUY': 1, 'SELL': 16, 'NEUTRAL': 9}
4h {'RECOMMENDATION': 'SELL', 'BUY': 1, 'SELL': 15, 'NEUTRAL': 10}

```

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [8 November 2022 21:40 UTC](https://discourse.nodered.org/t/parsing-converting-string-to-object/70281/6 "2022-11-08T21:40:07Z")

</div>

> This is how it's coming in raw

Question is why you get this weird output, the program producing it should be able to produce json as well, as it already partly does it.

---

<div class="post-metadata">

### Author: ![interscope101](https://avatars.discourse-cdn.com/v4/letter/i/a88e4f/32.png) [@interscope101](https://discourse.nodered.org/u/interscope101)
#### Post date: [8 November 2022 21:46 UTC](https://discourse.nodered.org/t/parsing-converting-string-to-object/70281/7 "2022-11-08T21:46:22Z")

</div>

it's calling on an external python scripts, that's how it come in, I have no control over it

---

<div class="post-metadata">

### Author: ![interscope101](https://avatars.discourse-cdn.com/v4/letter/i/a88e4f/32.png) [@interscope101](https://discourse.nodered.org/u/interscope101)
#### Post date: [8 November 2022 22:06 UTC](https://discourse.nodered.org/t/parsing-converting-string-to-object/70281/8 "2022-11-08T22:06:31Z")

</div>

Hi, just tried the flow ... getting "Unexpected end of JSON input" error

---

<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: [8 November 2022 22:11 UTC](https://discourse.nodered.org/t/parsing-converting-string-to-object/70281/9 "2022-11-08T22:11:59Z")

</div>

Your exec node output has a newline at end this should remove it also

```auto
[{"id":"550f90e56d2a20f8","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":3240,"wires":[["4a1e21f38e83239f"]]},{"id":"4a1e21f38e83239f","type":"template","z":"452103ea51141731","name":"simulate exec node output","field":"payload","fieldType":"msg","format":"handlebars","syntax":"plain","template":"5m {'RECOMMENDATION': 'SELL', 'BUY': 2, 'SELL': 15, 'NEUTRAL': 9}\n15m {'RECOMMENDATION': 'STRONG_SELL', 'BUY': 0, 'SELL': 16, 'NEUTRAL': 10}\n30m {'RECOMMENDATION': 'SELL', 'BUY': 3, 'SELL': 14, 'NEUTRAL': 9}\n1h {'RECOMMENDATION': 'SELL', 'BUY': 2, 'SELL': 15, 'NEUTRAL': 9}\n2h {'RECOMMENDATION': 'STRONG_SELL', 'BUY': 1, 'SELL': 16, 'NEUTRAL': 9}\n4h {'RECOMMENDATION': 'SELL', 'BUY': 1, 'SELL': 15, 'NEUTRAL': 10}\n","output":"str","x":340,"y":3240,"wires":[["3444de9788e63442"]]},{"id":"3444de9788e63442","type":"split","z":"452103ea51141731","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":90,"y":3280,"wires":[["a3ccec3c7cdb1cf5"]]},{"id":"a3ccec3c7cdb1cf5","type":"switch","z":"452103ea51141731","name":"","property":"payload","propertyType":"msg","rules":[{"t":"neq","v":"","vt":"str"}],"checkall":"true","repair":true,"outputs":1,"x":210,"y":3280,"wires":[["b8538daefdd37bf3"]]},{"id":"b8538daefdd37bf3","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"'","fromt":"str","to":"\"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":" {","fromt":"str","to":"\": {","tot":"str"},{"t":"set","p":"parts.ch","pt":"msg","to":", \"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":3280,"wires":[["881fc66a5dbb5c7e"]]},{"id":"881fc66a5dbb5c7e","type":"join","z":"452103ea51141731","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":510,"y":3280,"wires":[["80e95241cd91f75a"]]},{"id":"80e95241cd91f75a","type":"template","z":"452103ea51141731","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":" {\"{{{payload}}} }","output":"json","x":640,"y":3280,"wires":[["0d17775e4e4e5987"]]},{"id":"0d17775e4e4e5987","type":"debug","z":"452103ea51141731","name":"debug 106","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":490,"y":3340,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![interscope101](https://avatars.discourse-cdn.com/v4/letter/i/a88e4f/32.png) [@interscope101](https://discourse.nodered.org/u/interscope101)
#### Post date: [8 November 2022 22:21 UTC](https://discourse.nodered.org/t/parsing-converting-string-to-object/70281/10 "2022-11-08T22:21:37Z")

</div>

Thank you so 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: [4 December 2022 22:35 UTC](https://discourse.nodered.org/t/parsing-converting-string-to-object/70281/13 "2022-12-04T22:35:46Z")

</div>

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