JSON to array

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

JSON to array

Postby nick1461658753 » Tue Nov 07, 2017 3:03 pm

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?
How about pizza?
nick1461658753
 
Posts: 28
Joined: Tue Apr 26, 2016 10:19 am

Re: JSON to array

Postby ROCLASI » Tue Nov 07, 2017 5:40 pm

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.
Robert Ivens
ROCLASI Software Solutions / JBS Group, Partner
SAN Developer / Servoy Valued Professional / Servoy Certified Developer
Twitter: @roclasi / @servoyforge
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5237
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: JSON to array

Postby patrick » Tue Nov 07, 2017 6:02 pm

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.
Patrick Ruhsert
Servoy DACH
patrick
 
Posts: 3630
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Re: JSON to array

Postby nick1461658753 » Wed Nov 08, 2017 1:24 am

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.


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

Code: Select all
   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] );


Code: Select all
[{"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?
How about pizza?
nick1461658753
 
Posts: 28
Joined: Tue Apr 26, 2016 10:19 am

Re: JSON to array

Postby patrick » Wed Nov 08, 2017 11:33 am

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
Patrick Ruhsert
Servoy DACH
patrick
 
Posts: 3630
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Re: JSON to array

Postby nick1461658753 » Wed Nov 08, 2017 12:15 pm

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



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
Code: Select all
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:
Code: Select all
old_json_array.forEach(temp);


but this doesn't
Code: Select all
old_json_array[0].forEach(temp);
How about pizza?
nick1461658753
 
Posts: 28
Joined: Tue Apr 26, 2016 10:19 am

Re: JSON to array

Postby ROCLASI » Wed Nov 08, 2017 3:27 pm

Hi Nick,

You can get the keys from an object like so:
Code: Select all
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.
Robert Ivens
ROCLASI Software Solutions / JBS Group, Partner
SAN Developer / Servoy Valued Professional / Servoy Certified Developer
Twitter: @roclasi / @servoyforge
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5237
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: JSON to array

Postby patrick » Wed Nov 08, 2017 6:42 pm

or maybe even simpler

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


forEach() is a method of Array, not Object...
Patrick Ruhsert
Servoy DACH
patrick
 
Posts: 3630
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Re: JSON to array

Postby nick1461658753 » Thu Nov 09, 2017 2:34 pm

Yes this really helps :)

thx!
How about pizza?
nick1461658753
 
Posts: 28
Joined: Tue Apr 26, 2016 10:19 am


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 2 guests