I work on Epic and use Mumps here and there, I write some routines for all sorts of stuff in Epic, but I rarely use classes to do much. I know there are some class files out there that you can use to do JSON to Global processing but since I only have a certain level of access I can't load new classes. Since I couldn't do that and the client I am working for doesn't have the version of Intersystem Cache that does JSON I went ahead and wrote my own way of parsing a JSON response. You will see my code below which is written all inline, since sometimes I don't want to create a Routine that might be seen by the Cache Admins and raise all sorts of flags.
So first you have to get the JSON
Code:
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()
This is a free API used to get Federal Poverty Level, the r variable will look like this:
Code:
{
"amount": 20090,
"amount_nice": "$20,090",
"fpl_percentage": 59.73,
"in
fo": {
"year_base": 11770,
"year_rate": 4160
},
"request": {
"house
hold_size": "3",
"income": 1000.0,
"year": "2015"
}
}
Now lets use my code to parse the JSON and put it into a Global
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="""""""")!(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
If you really want to test it on a bigger JSON response then check out the free
http://www.weatherunderground.com API, it has other data types like URLs that I tested my code against. My code might be able to be slim-lined more, but right now it works great for what I need. I am going to put it into a Routine when I get a chance. Hope this helps someone.