It is currently Fri Nov 24, 2017 5:00 pm


All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: JSON to Global from HTTP Request
PostPosted: Wed Jun 15, 2016 3:21 pm 

Joined: Wed Jun 01, 2016 8:12 am
Posts: 2
Real Name: Christopher Childs
Began Programming in MUMPS: 03 Jan 2007
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.


Top
Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net