Converting String into JSON Object

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?

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?

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

[{"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"]]}]

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. :worried:

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.

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.

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 :

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.

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 Tags and that is done in the first change rule:
Screen Shot 2022-12-11 at 5.15.33 AM

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

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

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 Nones 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
could be merged into one:
Screen Shot 2022-12-11 at 5.38.52 AM
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:

{
  "_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.

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.

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