Help with converting strings to json objects

I want to create a json object for the following data. The 1st 2 characters are the actual keys and the rest of the data is the value. To make matters even more fun, the order of the data changes. I know there has to be a way to do this. I am just drawing a blank.

BN09"

9/2/2024, 4:59:02 PM[node: Message debug]

"FL00"

9/2/2024, 4:59:02 PM[node: Message debug]

"TM029"

9/2/2024, 4:59:02 PM[node: Message debug]

"FC00"

9/2/2024, 4:59:02 PM[node: Message debug]

"OS0"

9/2/2024, 4:59:02 PM[node: Message debug]

"WS000 000"

9/2/2024, 4:59:02 PM[node: Message debug]

"PJ104"

9/2/2024, 4:59:02 PM[node: Message debug]

"AL210"

9/2/2024, 4:59:02 PM[node: Message debug]

"VI731 000"

9/2/2024, 4:59:02 PM[node: Message debug]

"ON1"

I

I assume you mean a javascript object - rather than a JSON object ?

When you say the order changes - how is this identified ? or do you mean the order of messages coming in ?

Is each of the items above a seperate message that is coming in ?

Can you show us what nodes you have used to get it to this state ?

Are you essentially saying you want to split after the first 2 characters each time and stored the remainder as the value ? WIll subsequent duplicate keys in the object be allowed to overwrite the previous values ?

Have a look at the split node as it should do what you want - based on each of these being a discrete input message

Craig

Try this function:

const key = msg.payload.substring(0, 2)
const value = msg.payload.substring(2)
msg.payload = {}
msg.payload[key] = value
return msg;
2 Likes

Maybe this will jog your gray matter

[{"id":"03a012017f1aab1c","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"PM","x":90,"y":60,"wires":[["f6a7b940652bb595"]]},{"id":"f6a7b940652bb595","type":"template","z":"d1395164b4eec73e","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"BN09\nFL00\nTM029\nFC00\nOS0\nWS000 000\nPJ104\nAL210\nVI731 000\nON1","output":"str","x":240,"y":60,"wires":[["294f4fb12b87cbd7"]]},{"id":"294f4fb12b87cbd7","type":"split","z":"d1395164b4eec73e","name":"simulate incoming data","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":470,"y":60,"wires":[["0873c4f2f8b8663b"]]},{"id":"0873c4f2f8b8663b","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"$substring($$.payload,0,2)","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$substring($$.payload,2)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":120,"wires":[["b858c660b6fa37e7"]]},{"id":"b858c660b6fa37e7","type":"join","z":"d1395164b4eec73e","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":530,"y":120,"wires":[["811efaebc59d6068"]]},{"id":"811efaebc59d6068","type":"debug","z":"d1395164b4eec73e","name":"debug 2565","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":120,"wires":[]}]
1 Like

Thank you. That did the trick.

I have a question regarding the JSONata code you provided..

$substring($$.payload,0,2)

I looked up what the $$ is for and found:

  • $ The root of the input JSON. Only needed if you need to break out of the current context to temporarily navigate down a different path. E.g. for cross-referencing or joining data. Examples

What exactly does the $$ do?

$$ can be omitted in this expression, but in more complicated expressions it may be required to donate the root context (i.e msg). a single $ donates the context you are currently in (i.e a map loop). So if you where in a map loop $ would be the map context, and if you wanted to use msg context then you would use $$.
So in short it is saying msg.payload.

1 Like