Parsing a Json file in to a SAS dataset using SAS PROC DS2
<p>I am trying to parse JSON file and store the data in a SAS dataset Here is the sample of JSON file .</p>

<p>Right now I'm only trying to parse S array as an initial step. It has 2 objects so the output SAS dataset(Final goal is to create an hash table) would have 2 observations as shown below.</p>

<pre><code>1   14162   E       NG     1455209    98
2   14163   EN      NGG    1455210    784  
</code></pre>

<p>Here is the code that I tried....but not getting any rows. As DS2 is new in SAS 9.4M3, i couldn't find much documentation aswell. I guess it's going wrong at <code>"do while (j.ISRIGHTBRACE(tokenType));"</code> but not sure.</p>

<pre><code>proc ds2;
data Snaps (overwrite=yes);
dcl package json j();
dcl nvarchar(30)  Key value;
dcl varchar(1000000) character set utf8 response;
dcl int rc;

dcl package http webQuery();
dcl int rc tokenType parseFlags;
dcl nvarchar(128) token;
dcl integer i rc;
drop response rc;
retain i 0;

method init();
webQuery.createGetMethod('http://XXXXXXXX.com:9090/query?Date=20160211');
webQuery.executeMethod();
webQuery.getResponseBodyAsString(response, rc);
rc = j.createParser( response );
do while (rc = 0);
     i=i+1;
     j.getNextToken( rc, token, tokenType, parseFlags);
     put token=;
     if lowcase(strip(token)) = 'shots' then do;
     put 'inside shots =-----------------------------------------';
           j.getNextToken( rc, token, tokenType, parseFlags);
           put '--------------------ARRAY---------------------' token=;
           if j.ISLEFTBRACKET(tokenType) then do; *Entered in to an Array;
                j.getNextToken( rc, token, tokenType, parseFlags);
                put '------------------OBJECT-----------------------' token=;
                if j.ISLEFTBRACE(tokenType) then do;*Entered in to an Object;
                     do while (j.ISRIGHTBRACE(tokenType));
                           j.getNextToken( rc, token, tokenType, parseFlags);
                           put '------------------KEY-----------------------' token=;
                           if j.ISSTRING(tokenType) then do;
                                                Key=token;
                                                     j.getNextToken( rc, token, tokenType, parseFlags);
                                                     put '---------------------VALUE--------------------' token=;
                                                Value=token;
                                                Output;

                           end;
                     end;
                end;
           end;
     end; 
    if i &gt; 100 then stop;
end;

end; * method init;
method term();
    rc = j.destroyParser();
end;

enddata;
run;
quit;
</code></pre>

<p>Thanks.........</p>