Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JSON parser

535 views
Skip to first unread message

Sid

unread,
Mar 25, 2015, 1:03:53 PM3/25/15
to
Does anybody have a MUMPS Routine that parses JSON strings into an array or global?
Thanks

Seth Rylan Gainey

unread,
Mar 25, 2015, 2:59:57 PM3/25/15
to
On Wednesday, March 25, 2015 at 1:03:53 PM UTC-4, Sid wrote:
> Does anybody have a MUMPS Routine that parses JSON strings into an array or global?
> Thanks

The most robust parser we found is part of the VPR codebase. See https://github.com/OSEHRA-Sandbox/Health-Management-Platform/tree/master/hmp/hmp-main/src/main/mumps/dbj

JSONVPR>n VAR, JSON, ERR
JSONVPR 1S1>s JSON="{""afield"":""avalue"", ""array"":[{""morefields"":""morevalues""}]}"
JSONVPR 1S1>d DECODE^VPRJSON("JSON","VAR","ERR")
JSONVPR 1S1>zw VAR
VAR("afield")="avalue"
VAR("array",1,"morefields")="morevalues"

Documentation is in VPRJSON.int.

- Seth

rtweed

unread,
Mar 25, 2015, 3:57:58 PM3/25/15
to

Sid

unread,
Mar 26, 2015, 4:11:47 AM3/26/15
to
Thank you Seth.

Sid

unread,
Mar 26, 2015, 4:12:30 AM3/26/15
to
On Wednesday, March 25, 2015 at 9:57:58 PM UTC+2, rtweed wrote:
> https://github.com/robtweed/EWD/blob/master/_zewdJSON.m

Thanks Rob.

Astute Semantics

unread,
Mar 26, 2015, 12:14:28 PM3/26/15
to
Hello, Sid -

I am another Sid :-)

What is your full name and organization?

Welcome to the community,

Sidney Tarason

christophe...@gmail.com

unread,
Jun 16, 2016, 1:09:43 PM6/16/16
to
On Wednesday, March 25, 2015 at 1:03:53 PM UTC-4, Sid wrote:
> Does anybody have a MUMPS Routine that parses JSON strings into an array or global?
> Thanks

I know you asked about a Routine, but if you ever do need some in line code to handle it, I wrote something that can be pasted into the command line. I work with Epic and sometimes I don't have the ability to install a Routine. I use this code in a Print Group to call a Federal Poverty Index API and return some data, I also use it as a macro code to show the current temperature and conditions outside by calling the wunderground.com API :)

Get the JSON:
s server="fplapi.herokuapp.com",path="/api?size=3&income=1000&income_type=monthly",httprequest=##class(%Net.HttpRequest).%New(),httprequest.Server=server d httprequest.Get(path) s r=httprequest.HttpResponse.Data.Read()

Parse the JSON into a local variable called Rarray:

s st="f i=1:1:len s ch=$e(r,i) q:ch=""{""",ld="s level=level-1,j=i+1 f i=j:1:len s ch=$e(r,i) q:(ch=""}"")!(ch=""{"")!(ch="","")!(ch="""""""")!(ch="":"")",rs="s level=1,s=""""""""_$p($e(r,i,len),"""""""",2)_"""""""",j=i i 1 {f i=j:1:len s ch=$e(r,i) q:ch="":""} i 1 s ref=""Rarray(""_s_"")"",@ref=1",cc="s j=i+1,val="""" i 1 {f i=j:1:len s ch=$e(r,i) q:(ch=""}"")!(ch=""{"")!(ch="","")!(ch="""""""") s:(i=j)&(ch'="" "") val=val_ch s:(i>j) val=val_ch} i 1 s ref=""Rarray(""_s_"")"",@ref=$p(val,$c(10),1)",qc="s j=i+1,val="""",stop="""""""" i 1 {f i=j:1:len s ch=$e(r,i) s:ch="""""""" stop=1,ch=$e(r,i+1),i=i+1 q:stop=1 s val=val_ch} i 1 s ref=""Rarray(""_s_"")"",@ref=val i (ch'="","") s j=i f i=j:1:len s ch=$e(r,i) q:(ch="":"")!(ch="","")!(ch=""{"")!(ch=""}"")",lsq="s level=level+1,s=s_"",""""""_$p($e(r,i,len),"""""""",2)_"""""""",j=i i 1 {f i=j:1:len s ch=$e(r,i) q:ch="":""} i 1 s ref=""Rarray(""_s_"")"",@ref=1",cmc="s j=i+1,s=""""""""_$p($e(r,i,len),"""""""",2)_"""""""",ref=""Rarray(""_s_"")"",@ref=1,@ref=1 f i=j:1:len s ch=$e(r,i) q:ch="":""",lc="s j=i+1,s=s_"",""""""_$p($e(r,i,len),"""""""",2)_"""""""" f i=j:1:len s ch=$e(r,i) q:ch="":""",bs="f jj=1:1:level-1 s:jj=1 ns=ns_$p(s,"","",jj) s:jj>1 ns=ns_"",""_$p(s,"","",jj)",len=$L(r),level="",ch="",s="",ns="",val="" x st f q:i=len x:(ch="}") ld x:(ch="{")&(level="") rs x:(ch="{")&(level=1) lsq x:(ch="{")&(level>1) lsq x:ch=":" cc x:ch="""" qc s ns="" x:(ch=",")&(level>1) bs s:(ch=",")&(level>1) s=ns x:(ch=",")&(level>1) lc x:ch="," cmc

I have tested it on the weather API JSON response, which is a good amount of data, and it seems to get the structure perfectly. It should work correctly for any correctly formatted JSON.

I also just wanted to say I saw your stuff Rob Tweed after I wrote this and I might not have wasted so much time if I had saw your stuff first. I love that that you are a proponent for using Mumps (M) as a database. I think I am a fan of yours now.

Terry Wiechmann

unread,
Jun 16, 2016, 9:24:29 PM6/16/16
to
On Wednesday, March 25, 2015 at 1:03:53 PM UTC-4, Sid wrote:
> Does anybody have a MUMPS Routine that parses JSON strings into an array or global?
> Thanks

Encode and Decode: https://github.com/lparenteau/DataBallet/blob/master/r/json.m

rtweed

unread,
Jun 17, 2016, 5:13:38 AM6/17/16
to
On Thursday, 16 June 2016 18:09:43 UTC+1, Christopher Childs wrote:
>
> I know you asked about a Routine, but if you ever do need some in line code to handle it, I wrote something that can be pasted into the command line. I work with Epic and sometimes I don't have the ability to install a Routine. I use this code in a Print Group to call a Federal Poverty Index API and return some data, I also use it as a macro code to show the current temperature and conditions outside by calling the wunderground.com API :)

>
> I also just wanted to say I saw your stuff Rob Tweed after I wrote this and I might not have wasted so much time if I had saw your stuff first. I love that that you are a proponent for using Mumps (M) as a database. I think I am a fan of yours now.

Many thanks! About time Epic migrated to Node.js and used EWD 3 :-)

Christopher Childs

unread,
Jun 29, 2016, 2:56:48 PM6/29/16
to
I had to fix the code because it messed up when it came upon a blank node. Below is the new code.

s st="f i=1:1:len s ch=$e(r,i) q:ch=""{""",ld="s level=level-1,j=i+1 f i=j:1:len s ch=$e(r,i) q:(ch=""{"")!(ch="","")!(ch="""""""")!(ch="":"")",rs="s level=1,s=""""""""_$p($e(r,i,len),"""""""",2)_"""""""",j=i i 1 {f i=j:1:len s ch=$e(r,i) q:ch="":""} i 1 s ref=""Rarray(""_s_"")"",@ref=1",cc="s j=i+1,val="""" i 1 {f i=j:1:len s ch=$e(r,i) q:(ch=""}"")!(ch=""{"")!(ch="","")!(ch="""""""") s:(i=j)&(ch'="" "") val=val_ch s:(i>j) val=val_ch} i 1 s ref=""Rarray(""_s_"")"",@ref=$p(val,$c(10),1)",qc="s j=i+1,val="""",stop="""""""" i 1 {f i=j:1:len s ch=$e(r,i) s:ch="""""""" stop=1,ch=$e(r,i+1),i=i+1 q:stop=1 s val=val_ch} i 1 s ref=""Rarray(""_s_"")"",@ref=val i (ch'="","") s j=i f i=j:1:len s ch=$e(r,i) q:(ch="":"")!(ch="","")!(ch=""{"")!(ch=""}"")",lsq="s level=level+1,nx=$p($e(r,i,len),"""""""",1) s:nx[""}"" ch=""}"" q:nx[""}"" s s=s_"",""""""_$p($e(r,i,len),"""""""",2)_"""""""",j=i i 1 {f i=j:1:len s ch=$e(r,i) q:ch="":""} i 1 s ref=""Rarray(""_s_"")"",@ref=1",cmc="s j=i+1,s=""""""""_$p($e(r,i,len),"""""""",2)_"""""""",ref=""Rarray(""_s_"")"",@ref=1,@ref=1 f i=j:1:len s ch=$e(r,i) q:ch="":""",lc="s j=i+1,s=s_"",""""""_$p($e(r,i,len),"""""""",2)_"""""""" f i=j:1:len s ch=$e(r,i) q:ch="":""",bs="f jj=1:1:level-1 s:jj=1 ns=ns_$p(s,"","",jj) s:jj>1 ns=ns_"",""_$p(s,"","",jj)",len=$L(r),level="",ch="",s="",ns="",val="" x st f q:i=len x:(ch="}") ld x:(ch="{")&(level="") rs x:(ch="{")&(level=1) lsq x:(ch="{")&(level>1) lsq x:ch=":" cc x:ch="""" qc s ns="" x:(ch=",")&(level>1) bs s:(ch=",")&(level>1) s=ns x:(ch=",")&(level>1) lc x:ch="," cmc

michoe...@gmail.com

unread,
Jul 22, 2016, 10:05:02 AM7/22/16
to
The routines posted below are very helpful. Is there a standard enhancement to them, in VPR or ewd, to get data from a Fileman file and move it right into a JSON string? Basically GETS^DIQ, or FIND^DIC, or LIST^DIC, plus one of these other routines to make JSON out of it? You tell it the fields (and some way of identifying the record(s)), and it returns a JSON string.
What I'm really asking for is an equivalent of something like (for example) a php lookup from a ColdFusion page, where the query on one end just specifies the fields, the php lookup on the back-end gets them, and has an option to pack them into JSON to send back to the query. Neither end requires the programmer in the middle to hand-manipulate the data to get it ready for JSON; the most you need to do is examine the resulting JSON on the front-end to figure out how to get your data out.
Thanks,
Michael Reach

rtweed

unread,
Jul 22, 2016, 10:35:02 AM7/22/16
to
EWD.js and the newer EWD 3 will both automatically map data in global to jSON and vice versa. It's all actually done in JavaScript and not COS/Mumps code. See http://gradvs1.mgateway.com/download/ewd-document-store.pdf

You might also want to check out: https://github.com/robtweed/ewd-qoper8-vistarpc
to save you a lot of work accessing VistA RPC data

Sam Habiel

unread,
Jul 23, 2016, 4:30:18 PM7/23/16
to
I started something to that effect for the M-Web-Server:

https://github.com/shabiel/fileman-web
https://github.com/shabiel/M-Web-Server
0 new messages