JSON to array

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?

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.

also plain JavaScript JSON.parse() will work… The serialize plugin handles a few of Servoy’s objects (not known to plain JavaScript) as well, but for primitive arrays JSON.parse() (as opposed to JSON.stringify()) will do.

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

I’m trying the plugins.serialize.fromJSON now, it did not show up when I entered JSON in the searchbar, but managed to find it after a while.
But I’m having some trouble, I’m trying to decode a json string I get form the database

	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]

I don’t seem to get the array that I want, tried a few ways now. Any sugesstions?

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:

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

var xAsString = JSON.stringify(x)

Which will give you this

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

var xFromString = JSON.parse(xAsString);

and ask

xFromString.length → 2
xFromString[1].some_nested_object.nested_property_2 → 2.2

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

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

var xAsString = JSON.stringify(x)

Which will give you this

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

var xFromString = JSON.parse(xAsString);

and ask

xFromString.length → 2
xFromString[1].some_nested_object.nested_property_2 → 2.2

thanks this helps a lot.
Just one more question, the nested arrays are variable, so I don’t always know what the names of the values will be. (so it could be nested_property_1 or some_other_random_property_name.
I can reach this data now by saying something like this

application.output( old_json_array[0].cmc_thid )
or
application.output( old_json_array[0]["cmc_thid"] )

But I can’t find a way to find the names, in this case cmc_thid, and a foreach does not seem to work either on this
this works:

old_json_array.forEach(temp);

but this doesn’t

old_json_array[0].forEach(temp);

Hi Nick,

You can get the keys from an object like so:

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]]);
}

Hope this helps.

or maybe even simpler

for ( var i in yourObject ) {
      application.output(i + ' ==> ' + yourObject[i]);
}

forEach() is a method of Array, not Object…

Yes this really helps :)

thx!