re; using JSON outside of ZEN

10 views
Skip to first unread message

ChrisB

unread,
May 16, 2013, 5:59:53 AM5/16/13
to InterSy...@googlegroups.com
Hi, Perhaps Rich could help me on 2 queries I have with formatting JSON text from a PICK record;
 
   1) How to format multi sub value fields in JSON
   2) I note from your example that multi value fields are enclosed with '[' & ']' as such;
 
        "PHONE":["203-234-1234","345-234-4533"],
       "PhoneType":["HOME","WORK"]
 
        My query involves the ',' after the first multi value fields (PHONE), does it indicate that these multi value fields are grouped together. If for example instead of the 'PhoneType' multi value fields I had another multivalue field was not associated with PHONE would the ',' be required,
 
Thanks, Chris

Rich Taylor

unread,
May 16, 2013, 10:19:58 AM5/16/13
to InterSy...@googlegroups.com

Chris,

 

As in MV Basic code when you deal with associated multivalue sets each field is treated as a collection by itself.  What you are seeing is the JSON representation of that.  Each Multivalued field becomes an array structure within the JSON.  Each field is separate from the others.  Your code would need to enforce the association.

 

You can contrast this to using Cache default storage where the way to create an collection of associated elements of data would be to create a serial object that contains properties for each element of data in the association.  Then in the parent class you create a “list of” those objects. 

 

Hope this helps

 

Richard S Taylor
Sales Engineer
InterSystems Corporation
Office: 443-340-8614
FAX: 440-815-5805

Advanced Software technologies for Breakthrough applications 

--
You received this message because you are subscribed to the Google Groups "InterSystems: MV Community" group.
To post to this group, send email to Cac...@googlegroups.com
To unsubscribe from this group, send email to CacheMV-u...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/CacheMV?hl=en
---
You received this message because you are subscribed to the Google Groups "InterSystems: MV Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to InterSystems-...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Chris B

unread,
May 16, 2013, 10:27:49 AM5/16/13
to InterSy...@googlegroups.com
Richard,
 
Thanks for your reply and that helps with my query regarding multi value fields but what is the convention used for formatting multi SUB value fields in JSON?
 
Thanks,  Chris
checked for viruses and spam by CanIt.
http://www.canit.3d.net.uk/
--
image001.jpg

Jason Warner

unread,
May 16, 2013, 11:42:10 AM5/16/13
to intersy...@googlegroups.com
Couldn't you just nest your array? In JSON, it is valid to nest arrays
inside of arrays. So for fields with SVM, you could do:

{
...
"Phone": [["123-456-7890", "456-789-0123"], ["789-012-3456"]],
"PhoneType": [["HOME", "WORK"], ["HOME"]],
...
}

In JSON, there are a couple of simple conventions:

"something": -- Denotes a property on an object
{} -- Everything inside the braces is an object
[] -- Everything inside of braces is an array

Properties can only exist inside of objects and are separated by commas.
Objects can be nested inside of properties or arrays
Arrays can be nested inside of properties or other arrays. Commas
separate values inside of arrays.

With these simple rules, JSON can represent some pretty complex objects.
As Rich said, the various MV marks really just denote collections which
are easily represented in JSON with arrays.

Jason

On Thursday, May 16, 2013 8:27:49 AM, Chris B wrote:
>
> Richard,
> Thanks for your reply and that helps with my query regarding multi
> value fields but what is the convention used for formatting multi SUB
> value fields in JSON?
> Thanks, Chris
> *From:* Rich Taylor <mailto:Rich....@intersystems.com>
> *Sent:* Thursday, May 16, 2013 3:19 PM
> *To:* InterSy...@googlegroups.com
> <mailto:InterSy...@googlegroups.com>
> *Subject:* RE: [InterSystems-MV] re; using JSON outside of ZEN
>
> Chris,
>
> As in MV Basic code when you deal with associated multivalue sets each
> field is treated as a collection by itself. What you are seeing is
> the JSON representation of that. Each Multivalued field becomes an
> array structure within the JSON. Each field is separate from the
> others. Your code would need to enforce the association.
>
> You can contrast this to using Cache default storage where the way to
> create an collection of associated elements of data would be to create
> a serial object that contains properties for each element of data in
> the association. Then in the parent class you create a “list of”
> those objects.
>
> Hope this helps
>
> Richard S Taylor
> Sales Engineer
> InterSystems Corporation
> Office: 443-340-8614
> FAX: 440-815-5805
>
> Advanced Software technologies for Breakthrough applications
>
> *From:*InterSy...@googlegroups.com
> [mailto:InterSy...@googlegroups.com] *On Behalf Of *ChrisB
> *Sent:* Thursday, May 16, 2013 6:00 AM
> *To:* InterSy...@googlegroups.com
> *Subject:* [InterSystems-MV] re; using JSON outside of ZEN
>
> Hi, Perhaps Rich could help me on 2 queries I have with formatting
> JSON text from a PICK record;
>
> 1) How to format multi sub value fields in JSON
>
> 2) I note from your example that multi value fields are enclosed
> with '[' & ']' as such;
>
> "PHONE":["203-234-1234","345-234-4533"],
>
> "PhoneType":["HOME","WORK"]
>
> My query involves the ',' after the first multi value fields
> (PHONE), does it indicate that these multi value fields are grouped
> together. If for example instead of the 'PhoneType' multi value fields
> I had another multivalue field was not associated with PHONE would the
> ',' be required,
>
> Thanks, Chris
>
> --
> You received this message because you are subscribed to the Google
> Groups "InterSystems: MV Community" group.
> To post to this group, send email to Cac...@googlegroups.com
> <mailto:Cac...@googlegroups.com>
> To unsubscribe from this group, send email to
> CacheMV-u...@googlegroups.com
> <mailto:CacheMV-u...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/CacheMV?hl=en
> ---
> You received this message because you are subscribed to the Google
> Groups "InterSystems: MV Community" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to InterSystems-...@googlegroups.com
> <mailto:InterSystems-...@googlegroups.com>.

Chris B

unread,
May 16, 2013, 12:23:40 PM5/16/13
to InterSy...@googlegroups.com
Thanks Jason, your explanation was very much appreciated, clear and concise
> the association. Then in the parent class you create a �list of�

Chris B

unread,
May 20, 2013, 5:10:33 AM5/20/13
to InterSy...@googlegroups.com
Jason/Rich,

Further to my enquiry last week regarding formating JSON text from Pick data
files with multi sub values; The example you provided is OK if you have a
multi value field and an associated multi sub value but how would you format
this if there were several multi sub values fields associated with one multi
value field.

I have a PICK file named HOTELS which has for example several multi sub
value fields associated with one multi value (see example)

Record Id > CUM
Hotel Name > CUMBERLAN
Room Category > BASIC]STANDARD]LUXURY
Room Codes > SIN\TWI] SIN\TWI\TRP]LSI\LTW\LDB
No Rooms > 10\15]5\5\5]3\3
No Avail > 5\4]1\2\3]3\3

The 3 multi sub value fields (Room Codes,No Rooms & No Avail) are all
associated with multi value field Room Category
How would such data be formatted in JSON is my query and your help would be
much appreciated,

Thanks, Chris

-----Original Message-----
From: Jason Warner
Sent: Thursday, May 16, 2013 4:42 PM
To: intersy...@googlegroups.com
> the association. Then in the parent class you create a �list of�

Jason Warner

unread,
May 20, 2013, 12:34:10 PM5/20/13
to intersy...@googlegroups.com
You could just keep nesting arrays. For example:

{
...
"RoomCodes": [["SIN", "TWI"], ["SIN", "TWI", "TRP"], ["LSI", "LTW",
"LDB"]]
"NoRooms": [[10, 15], [5, 5, 5], [3, 3]]
"NoAvail": [[5, 4], [1, 2, 3], [3, 3]]
...
}

I don't know that I would recommend having the ragged arrays. Since in
javascript accessing obj.RoomCodes[2,3] will give you "LDB". However,
obj.NoRooms[2,3] will throw an array index exception. I would recommend
giving that for null values, you give a default value. Another thing
you could do is create objects that would associate the items so you
don't have to keep the associations in your javascript app. For Example:

{
"RecordId": "CUM",
"HotelName": "CUMBERLAN",
"Rooms": [
{
"Category": "BASIC",
"Codes": ["SIN", "TWI"],
"NoRooms": [10, 15],
"NoAvail": [5, 4]
},
{
"Category": "STANDARD",
"Codes": ["SIN", "TWI", "TRP"],
"NoRooms": [5, 5, 5],
"NoAvail": [1, 2, 3]
},
{
"Category": "LUXURY",
"Codes": ["LSI", "LTW", "LDB"],
"NoRooms": [3, 3, 0],
"NoAvail": [3, 3, 0]
}
]
}

Jason
>> the association. Then in the parent class you create a “list of”

Chris B

unread,
May 21, 2013, 6:58:44 AM5/21/13
to InterSy...@googlegroups.com
Hi Rich,
 
We have now upgraded to Cache 2013.1 and I can now format a pick record to JSON but have 2 points; Rather than outputting the JSON text to the console I would like to be able to assign it to a PICK variable – how is this done? Also in my example below I am using the PICK file HOTELS, but how can I within a Pick program replace this dynamically with another file name such as RESORTS or COUNTRIES.
 
 
          RECID='HIL'
          HotelObj="MVFILE.HOTELS"->%OpenId(RECID)
          "%ZEN.Auxiliary.jsonProvider"->%WriteJSONStreamFromObject(jsonStream,HotelObj,,,,"cbwi")
          jsonStream->OutputToDevice()
 
Thanks, Chris
 
Sent: Thursday, May 16, 2013 3:19 PM
checked for viruses and spam by CanIt.
http://www.canit.3d.net.uk/
--
image001.jpg

Rich Taylor

unread,
May 21, 2013, 11:05:30 AM5/21/13
to InterSy...@googlegroups.com

Chris,

 

To put the JSON into a variable you would use the Read() or Readline() method of the stream class.  There is a caveat though.  The maximum string for in Caché 3.6 mb.  If your JSON is larger than this you will get an error.  If this is an issue then you will either need to change your code to work directly with the stream or break the JSON into pieces.  Why does this need to be in a variable?

 

To your question on dynamic class references when you try to open an object, in Caché Object Script there is a function for this;  $Classmethod().  Unfortunately this is not available in MV Basic.  Here is a way to get around this:

 

classname = "MVFILE.PERSON"

a = ";obj = ":quote(classname):"->%OpenId(2)"

Perform a

 

The Classname is assigned to a variable.  The a command line execution of a MV Basic statement (note the ; at the start of the line) that opens an instance of that class.  A PERFORM statement is then used to execute this within the context of the local variable environment.  The variable Obj will now have the handle to the MVFILE.PERSON object.

 

Rich

Chris B

unread,
Sep 1, 2012, 12:43:09 PM9/1/12
to InterSy...@googlegroups.com
Rich,
Thanks for your kind help on these matters which I have now resolved. Just out of interest I notice that the returned JSON formatted text does not deal with multi sub values but only the multi values.Will this be dealt with by Intersystems?
Thanks Chris
image001.jpg

Rich Taylor

unread,
May 23, 2013, 12:48:11 PM5/23/13
to InterSy...@googlegroups.com

At this time I am not sure of this.  I can see if a request has already been put in and do some additional checking myself to see if there is an alternative that would work today.  One thought is to use the JSON SQL provider to generated the JSON based on grandchild table queries.  This provider is available in 2013.1.

Rich Taylor

unread,
Jun 12, 2013, 6:57:35 AM6/12/13
to InterSy...@googlegroups.com

Chris,

 

Just wanted to follow up with you to be sure you got all the answers you needed from the responses here.  Let us know if you have any further questions.

 

Richard S Taylor
Sales Engineer
InterSystems Corporation
Office: 443-340-8614
FAX: 440-815-5805

Advanced Software technologies for Breakthrough applications 

 

From: InterSy...@googlegroups.com [mailto:InterSy...@googlegroups.com] On Behalf Of Chris B


Sent: Thursday, May 23, 2013 12:44 PM

Reply all
Reply to author
Forward
0 new messages