I have json strings in my database, but would like to convert them to arrays, but I can't find a good way to do it, I can find many ways to build a JSON string, but not to decode one.
So how do I decode a json string to an array in servoy?
ROCLASI wrote:Hi Nick,
You can use the bundled Serialize plugin or the VelocityReport plugin. Both have functions to convert from a String to JSON and back.
Hope this helps.
var new_array = new Array( );
new_array = plugins.serialize.fromJSON( queue_json );
application.output( queue_json );
application.output( "array length " + new_array.length );
application.output( "string " + new_array.toString() );
application.output( "array 0 " + new_array[0] );
[{"sync_queue_id":"E8465A5B-D5C1-43C2-8FBF-1519031F7419","sync_table":"admin_value","sync_pk_col":"cmc_thid","sync_pk":"057BB27A-5FCD-4903-8A4B-6B892E75C835","time_changed":"2017-11-07T22:53:07.293Z","in_progress":1,"completed":0,"data_old":"{\"cmc_thid\":\"057bb27a-5fcd-4903-8a4b-6b892e75c835\",\"key_admin_value\":\"Warehousing/unit t-c\",\"value_numeric\":0.4}","data_new":"{\"cmc_thid\":\"057bb27a-5fcd-4903-8a4b-6b892e75c835\",\"key_admin_value\":\"Warehousing/unit t-c\",\"value_numeric\":0.5}","operation":"UPDATE"}]
array length 1
string [[object Object]]
array 0 [object Object]
var x = [{some_property: 1, some_nested_object: {nested_property_1: '1.1', nested_property_2: '1.2'}}, {some_property: 2, some_nested_object: {nested_property_1: '2.1', nested_property_2: '2.2'}}]
var xAsString = JSON.stringify(x)
[{"some_property":1,"some_nested_object":{"nested_property_1":"1.1","nested_property_2":"1.2"}},{"some_property":2,"some_nested_object":{"nested_property_1":"2.1","nested_property_2":"2.2"}}]
var xFromString = JSON.parse(xAsString);
patrick wrote:It's a bit hard to really see what you are doing and if that array string you are converting from is really a JSON string etc. That you see "array 0 [object Object]" btw is perfectly normal. From what I can see your array[0] holds an object and the toString() method from a custom js object will return "[object Object]". If you output instead new_array[0].sync_queue_id you should get "E8465A5B-D5C1-43C2-8FBF-1519031F7419".
Let's look at a simple example and that maybe helps you to figure out what's not working the way you want. Let's say you have this:
- Code: Select all
var x = [{some_property: 1, some_nested_object: {nested_property_1: '1.1', nested_property_2: '1.2'}}, {some_property: 2, some_nested_object: {nested_property_1: '2.1', nested_property_2: '2.2'}}]
Then you can do
- Code: Select all
var xAsString = JSON.stringify(x)
Which will give you this
- Code: Select all
[{"some_property":1,"some_nested_object":{"nested_property_1":"1.1","nested_property_2":"1.2"}},{"some_property":2,"some_nested_object":{"nested_property_1":"2.1","nested_property_2":"2.2"}}]
Now you try
- Code: Select all
var xFromString = JSON.parse(xAsString);
and ask
xFromString.length -> 2
xFromString[1].some_nested_object.nested_property_2 -> 2.2
application.output( old_json_array[0].cmc_thid )
or
application.output( old_json_array[0]["cmc_thid"] )
old_json_array.forEach(temp);
old_json_array[0].forEach(temp);
var _oValues = {prop1:"val1",prop2:"val2", prop3:"val3"},
_aKeys = Object.keys(_oValues);
for ( var i = 0 ; i < _aKeys.length ; i++ ){
application.output(_akeys[i] + " ==> " + _oValues[_aKeys[i]]);
}
for ( var i in yourObject ) {
application.output(i + ' ==> ' + yourObject[i]);
}
Return to Programming with Servoy
Users browsing this forum: No registered users and 25 guests