Convert an array to a JSON string
I need to convert an array with an unknown number of indices to a JSON string for transmission (which is why it isn't possible to iterate through it using $ORDER). Does ObjectScript provide functionality to convert an array to a JSON string?
Edit 1: As Joel mentioned, the array is subscripted and has an arbitrary structure like:
a(1)="a" a(3,4)="b" a(3,6)="c" a(5,6,7)="d"
which needs to be converted to
{"1":"a", "3":{"4":"b","6":"c"}, "5":{"6":{"7":"d"}}}
I had considered using a combination of $Query (to traverse the array) and $Order (to retrieve property names) but was wondering if a utility method already exists.
IRIS for UNIX (Apple Mac OS X for x86-64) 2019.4.0DS (Build 165U) Fri Aug 30 2019 00:02:44 EDT
I would expect %JSONAdapter. %JSONExportToString() should be the function to serialize arrays to string;
extend or wrap your array into a object that inherits from %JSON.Adaptor and serialize it...
btw. the Array Objects do have "Next or GetAt" function to iterate as well (look at the matching classdocumentation)
If you have old version of Caché, you can use %ZEN.Auxiliary.jsonProvider or %ZEN.Auxiliary.altJSONProvider, which have a bunch of useful methods, for example:
%ArrayToJSON
%WriteJSONFromArray
%WriteJSONStreamFromArray
etc.
Here are two small examples:
For old Caché versions there is
https://github.com/PlanetCache/CacheJSON
We may need more clarity in order to answer this question. This is what I think you mean. You want to take an ObjectScript array of arbitrary structure, like this:
...and turn it into JSON. But what should the target JSON for this example look like? Something like this?
...or something different?
In any case, to loop through an ObjectScript array of arbitrary structure, you need to use $Query.
I would recommend a different structure to the JSON since an array node can hold both, a value and subnodes. This should be reflected in the JSON. So, for an array:
a(1)="A"
a(1,1)="AA"
a(1,2)="AB"
a(1,2,1)="ABA"
a(1,3,1)="ACA"
a(2,1)="BA"
a(3)="C"
a(3,1)="CA"
I suggest to generate an JSON in the form:
{"1":["A",{"1":["AA"],"2":["AB",{"1":["ABA"]}],"3":[{"1":["ACA"]}]}],"2":[{"1":["BA"]}],"3":["C",{"1":["CA"]}]}
This may not be the most convenient for the receiving end to read, but it can hold the full information from the array. This can easily be created using a recursive procedure:
See:
Social networks
InterSystems resources
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue