# Converting String into JSON Object

**URL:** <https://discourse.nodered.org/t/converting-string-into-json-object/72002>\
**Category:** General\
**Created:** [10 December 2022 18:53 UTC](https://discourse.nodered.org/t/converting-string-into-json-object/72002 "2022-12-10T18:53:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![damica51](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@damica51](https://discourse.nodered.org/u/damica51)\
**Post date:** [10 December 2022 18:53 UTC](https://discourse.nodered.org/t/converting-string-into-json-object/72002/1 "2022-12-10T18:53:50Z")

</div>

I've been working on this for some time and I can not figure out how to come up with a solution.

I am trying to format this string:  
[Tag(tag='DINT1', value=20, type='DINT', error=None), Tag(tag='SINT1', value=5, type='SINT', error=None), Tag(tag='REAL1', value=100.0009994506836, type='REAL', error=None)]

Into a format like this:  
DINT1, 20, DINT, None  
SINT1, 5, SINT, None  
REAL1, 100.000994, Real, None

Any suggestion?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 December 2022 21:08 UTC](https://discourse.nodered.org/t/converting-string-into-json-object/72002/2 "2022-12-10T21:08:34Z")

</div>

My first question: is where does that data come form?  
Second question: do you have any control over the generatin of the data?  
Third question: is that the exact format you want the result to be?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 December 2022 21:40 UTC](https://discourse.nodered.org/t/converting-string-into-json-object/72002/3 "2022-12-10T21:40:13Z")

</div>

If the format stays the same, you could do this with a `change` node and a `json` node

```auto
[{"id":"006ee02eff444905","type":"change","z":"b6e35894823650c4","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"Tag","fromt":"str","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"(","fromt":"str","to":"{","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"tag=","fromt":"str","to":"\"tag\":","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"'","fromt":"str","to":"\"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"value=","fromt":"str","to":"\"value\":","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"type=","fromt":"str","to":"\"type\":","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"error=","fromt":"str","to":"\"error\":","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"None),","fromt":"str","to":"\"None\"},","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"None)]","fromt":"str","to":"\"None\"}]","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":880,"wires":[["0fdea342bae15836"]]},{"id":"900cbd7dec7ad23b","type":"inject","z":"b6e35894823650c4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[Tag(tag='DINT1', value=20, type='DINT', error=None), Tag(tag='SINT1', value=5, type='SINT', error=None), Tag(tag='REAL1', value=100.0009994506836, type='REAL', error=None)]","payloadType":"str","x":190,"y":880,"wires":[["006ee02eff444905","1a040f4790aa45b8"]]},{"id":"e7cd9a9616eccb38","type":"debug","z":"b6e35894823650c4","name":"debug 25","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":680,"y":880,"wires":[]},{"id":"1a040f4790aa45b8","type":"debug","z":"b6e35894823650c4","name":"debug 26","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":360,"y":940,"wires":[]},{"id":"0fdea342bae15836","type":"json","z":"b6e35894823650c4","name":"","property":"payload","action":"","pretty":false,"x":530,"y":880,"wires":[["e7cd9a9616eccb38"]]}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 December 2022 21:49 UTC](https://discourse.nodered.org/t/converting-string-into-json-object/72002/4 "2022-12-10T21:49:21Z")

</div>

Oh good grief!

I've been slowly working towards an answer.  
So far I have 2 splits, a switch, a change, another split, a join and a function (damn it's eyes).  
The json node still won't accept it as valid.

I'm not sure you should assume **error** is always **None** without quotes though. Could it be **Unreal** , also without quotes?.(Hence my function: msg.payload.pop() )

ANd now I notice that the value of **error** is required in the output anyway. 😟

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 December 2022 21:54 UTC](https://discourse.nodered.org/t/converting-string-into-json-object/72002/5 "2022-12-10T21:54:47Z")

</div>

> [@jbudd](#):
>
> So far I have 2 splits, a switch, a change, another split, a join and a function (damn it's eyes).  
> The json node still won't accept it as valid.

hahaha my original version has a split, switch. change, json, and join.

When @damica51 answers my original questions we'll know more of what needs to be done, but this should give him an idea of how it can be done.

---

<div class="post-metadata">

**Author:** ![damica51](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@damica51](https://discourse.nodered.org/u/damica51)\
**Post date:** [10 December 2022 21:58 UTC](https://discourse.nodered.org/t/converting-string-into-json-object/72002/6 "2022-12-10T21:58:50Z")

</div>

To answer the first question, this comes from reading tags out of a PLC with Python.  
I can't change the format of the returned string.  
Yes, I do need it to be as listed.

Your example works perfectly, but I will have to study your change node as there are several rules I really don't understand why you set them up that way. Please give me some time to study.

I really do appreciate your help I have worked on this for days.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 December 2022 23:26 UTC](https://discourse.nodered.org/t/converting-string-into-json-object/72002/7 "2022-12-10T23:26:18Z")

</div>

In order to turn that string into an object it has to be massaged. The terms on the left side of the equal sign needed to be in double quotes and the equal sign changed into a colon `:`

---

<div class="post-metadata">

**Author:** ![damica51](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@damica51](https://discourse.nodered.org/u/damica51)\
**Post date:** [11 December 2022 02:36 UTC](https://discourse.nodered.org/t/converting-string-into-json-object/72002/8 "2022-12-11T02:36:58Z")

</div>

So just to be clear, your not only formatting the string, your setting it up so it can be converted into "OBJECTs".  
I'm still trying to get my head around all of the rules and how you go about making the needed changes.

Thanks again for all your help.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 December 2022 10:49 UTC](https://discourse.nodered.org/t/converting-string-into-json-object/72002/9 "2022-12-11T10:49:07Z")

</div>

the way I looked at it was the string needed to be changed into an object so lets walk thru what I did. Your string is:  
`[Tag(tag='DINT1', value=20, type='DINT', error=None), Tag(tag='SINT1', value=5, type='SINT', error=None), Tag(tag='REAL1', value=100.0009994506836, type='REAL', error=None)]`

An array of objects needs to look like [{...}, {...}, {...}] so first thing it to get rid of all the `Tag`s and that is done in the first change rule:  
 ![Screen Shot 2022-12-11 at 5.15.33 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b56a9aeaa0bcb03123e52016464eeff2385a1e6e.png)

so the string is now:  
`[(tag='DINT1', value=20, type='DINT', error=None), (tag='SINT1', value=5, type='SINT', error=None), (tag='REAL1', value=100.0009994506836, type='REAL', error=None)]`

Each object needs to start with a `{`, the variable name need to be put in double quotes `"` and it needs a a colon `:` so `(tag=` has to be changed to `{"tag":` which is what the secong rule does:  
 ![Screen Shot 2022-12-11 at 5.21.51 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e7b61df806a50846a7727da81953055b3302170c.png)

and now you have:  
`[{"tag":'DINT1', value=20, type='DINT', error=None), {"tag":'SINT1', value=5, type='SINT', error=None), {"tag":'REAL1', value=100.0009994506836, type='REAL', error=None)]`

Rule 3 changes all the single quotes to double quotes  
 ![Screen Shot 2022-12-11 at 5.26.14 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/7/b71771cad4c3ac2f54610cf71bbc76aac3e01d44.png)  
so the string no looks like this  
`[{"tag":"DINT1", value=20, type="DINT", error=None), {"tag":"SINT1", value=5, type="SINT", error=None), {"tag":"REAL1", value=100.0009994506836, type="REAL", error=None)]`  
rule 4, 5 and 6 fix up `value=`, `type=` and `error=`  
 ![Screen Shot 2022-12-11 at 5.28.41 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/48b1e789bd51c0455694daaa70cca84ef18757fc.png)

so the string is now:  
`[{"tag":"DINT1", "value":20, "type":"DINT", "error":None), {"tag":"SINT1", "value":5, "type":"SINT", "error":None), {"tag":"REAL1", "value":100.0009994506836, "type":"REAL", "error":None)]`

The only thing left is to double quote the `None`s and chang the `)` to `}`

As I was writting this I realized that the lasst two rules can be changed to one. I was worried about the lase `None)]` while the rest were `None),` but I didn't need to worry about what was after the `)` so the original last two rules:  
 ![Screen Shot 2022-12-11 at 5.37.58 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/f/1f9cc6fc24a343790bf82ef0b3d9d1cb816a6724.png)  
could be merged into one:  
 ![Screen Shot 2022-12-11 at 5.38.52 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f4f1838a8157d852a5170c43548f79c331a7aca0.png)  
and the result is a JSON string  
`[{"tag":"DINT1", "value":20, "type":"DINT", "error":"None"}, {"tag":"SINT1", "value":5, "type":"SINT", "error":"None"}, {"tag":"REAL1", "value":100.0009994506836, "type":"REAL", "error":"None"}]`  
which fed thru the `json` node converts it to an array of objects:

```auto
{
  "_msgid" : "553f892d34eaf1d2",
  "payload" : [
    {
      "tag" : "DINT1",
      "value" : 20,
      "type" : "DINT",
      "error" : "None"
    },
    {
      "tag" : "SINT1",
      "value" : 5,
      "type" : "SINT",
      "error" : "None"
    },
    {
      "tag" : "REAL1",
      "value" : 100.0009994506836,
      "type" : "REAL",
      "error" : "None"
    }
  ],
  "topic" : ""
}

```

Hope that helps you understand how/why I used thoes rules in the `change` node.

---

<div class="post-metadata">

**Author:** ![damica51](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@damica51](https://discourse.nodered.org/u/damica51)\
**Post date:** [11 December 2022 11:10 UTC](https://discourse.nodered.org/t/converting-string-into-json-object/72002/10 "2022-12-11T11:10:37Z")

</div>

Thank you so very much for taking the time to explain this. Now I understand and this will help me as I work with Objects more to come.

---

<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:** [25 December 2022 11:11 UTC](https://discourse.nodered.org/t/converting-string-into-json-object/72002/11 "2022-12-25T11:11:03Z")

</div>

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