Working with JSON Objects
JSON objects and all other objects behave like the infotable objects; they are all clones.
For example:
The following code will cause unexpected behavior. It will appear to be set but does not actually persist the change if it is a persistent property. This is the same behavior you would see in ThingWorx 8.4. The in-memory version of the value is changed, but it does not persist correctly.
me.jsonProp['key'] = 'newValue';
To work with JSON objects correctly, you can assign a JSON string to the property value:
me.jsonProp= {'key':'newValue'};
Or you can make a copy of the property, make updates, and then assign them back to the original property:
var json = me.jsonProp;
json['key'] = 'newValue';
me.jsonProp = json;
All property values are stored in a cache layer. The object used in the JavaScript is only a wrapper to the object, which handles some writes back to the cache to help with legacy support. Only one level or wrapper is supported for direct object updates. It is important to understand that each assignment will write the entire object back to the cache, so you should always make a copy when you need to make multiple changes.
The following example does not work. The json object is a wrapper to the original object, but unit is a sub-object and is not a wrapper. So assigning .unit will not write the value back to the cached object:
input.recurrent.unit = "MB"
You can use one of the following workarounds:
Work on a sub-part of the document, and assign it back:
json = input.recurrent
json.unit = "MB";
input.recurrent = json <--- this does a cache update
If you have to update multiple values in the JSON, always make a copy of the entire object, work on it, and then assign it back for best performance.
json = JSON.parse(input);
json.recurrent.unit ="MB";
input = json <--- this does a cache update
You should limit cache writes for best performance. They may be written fairly fast in single server mode, but in high availability cluster mode, the cache is distributed so each write will have a performance impact.
input.value = 5 <--- this does a cache update
input.text = "junk" <--- this does a cache update
Was this helpful?