This line of code used to work, as in replacing the old processID with the new one.
‘systemstatus_thing’ holds the JSON-string fetched via a REST-call, ‘openHAB_PID’ holds the processID.
systemstatus_thing=systemstatus_thing.replaceAll('"pid":([0-9]+)','"pid":' + openHAB_PID);
Using it now inserts newlines after the processIDs, using the malformed string to update the thing fails. I could swear it worked before.
Why not parse the JSON, replace the field directly and recreate the JSON string?
parsed_systemstatus_thing = JSON.parse(systemstatus_thing);
parsed_systemstatus_thing.pid = openHAB_PID;
systemstatus_thing = JSON.stringify(parsed_systemstatus_thing);
REGEX is such a pain I try to avoid it where possible.
Thanks for this idea, sometimes one needs a knock on the head.
Why on earth does the suggested code add a pid field at the end of the JSON string including a ‘\n’?
The original string ends with
Hmmm, are you sure the newline isn’t in openHAB_PID? Are you getting that using executeCommandLine? There is almost always a newline at the end of the output from a command. In other words, your original problem is still there.
The order doesn’t matter with JSON. Remember you parsed it into a JS Object and then serialized it back to a String, you are not editing a string in place. There is no reason it has to write the values back out in the same order. It probably put it last because it changed which changed it’s position in memory.
You nailed it, as usual! Thanks.
The other problem ( the additional .pid field) was inserted as I should have specified the correct “sub” field in the whole JSON. Without such the above command just added this field at the end.