filename cp temp; proc groovy classpath=cp; add sasjar="groovy_2.1.3" version="2.1.3.0_SAS_20130517000930"; submit parseonly; import groovy.json.JsonSlurper class MyJsonParser { def parseFile(path) { def jsonFile = new File(path) def jsonText = jsonFile.getText() def InputJSON = new JsonSlurper().parseText(jsonText) def accounts = [] InputJSON.results.each{ accounts << [ acct_nbr : it.acct_nbr.toString(), firstName : it.firstName, lastName : it.lastName, age : it.age.toString(), streetAddress : it.address.streetAddress, city : it.address.city, state : it.address.state, postalCode : it.address.postalCode ] } return accounts } } endsubmit; submit parseonly; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; public class MyJsonParser4Sas { public String filename = ""; public void init() { MyJsonParser myParser = new MyJsonParser(); accounts = myParser.parseFile(filename); iter = accounts.iterator(); } public boolean hasNext() { return iter.hasNext(); } public void getNext() { account = ((LinkedHashMap) (iter.next())); } public String getString(String k) { return account.get(k); } protected ArrayList accounts; protected Iterator iter; protected LinkedHashMap account; } endsubmit; quit; options set=classpath "%sysfunc(pathname(cp,f))"; filename json "/home/mkastin/Desktop/foo.json"; data accounts; attrib id label="Account Index" length= 8 acct_nbr label="Account Number" length=$ 10 firstName label="First Name" length=$ 20 lastName label="Last Name" length=$ 30 age label="Age" length=$ 3 streetAddress label="Street Address" length=$ 128 city label="City" length=$ 40 state label="State" length=$ 2 postalCode label="Postal Code" length=$ 5; dcl javaobj accounts("MyJsonParser4Sas"); accounts.exceptiondescribe(1); accounts.setStringField("filename", "%sysfunc(pathname(json,f))"); accounts.callVoidMethod("init"); accounts.callBooleanMethod("hasNext",rc); do id=1 by 1 while(rc); accounts.callVoidMethod("getNext"); accounts.callStringMethod("getString", "acct_nbr", acct_nbr); accounts.callStringMethod("getString", "firstName", firstName); accounts.callStringMethod("getString", "lastName", lastName); accounts.callStringMethod("getString", "age", age); accounts.callStringMethod("getString", "streetAddress", streetAddress); accounts.callStringMethod("getString", "city", city); accounts.callStringMethod("getString", "state", state); accounts.callStringMethod("getString", "postalCode", postalCode); output; accounts.callBooleanMethod("hasNext",rc); end; drop rc; run; 1 1234 John Smith 25 21 2nd Street New York NY 10021 2 3456 Sam Jones 32 25 2nd Street New Jersy NJ 10081 A few notes: 1. The json as shared here is invalid and should be wrapped in "{}" 2. In windows you will need to modify the file's name in one of following ways 2a. escape the \ as \\, for example C:\json.txt would be C:\\json.txt 2b. use forward slash instead of backslash, for example C:\json.txt would be /C:/json.txt 2c. use a file uri specification ie. file://C:/json.txt 3. The add sasjar statement is specific to your version or SAS and OS, check your versioned jar repositiory as part of your installation or use a different groovy-all jar, as you had in OP
... View more