BookmarkSubscribeRSS Feed
bashique
Calcite | Level 5

How to parse json string which has more than 32767 characters in SAS?

 

sample of json string (Response key):

{"message"="1000 records found","status"="200","Respone"="[{\"name\":\"Jhon\",\"age\":22,\"class\":\"mca\"},{\"name\":\"Victor\",\"age\":22,\"class\":\"mca\"}]"}

 above sample json has been parsed in python using json.loads() function can anyone suggest the alternative in sas

6 REPLIES 6
Ksharp
Super User
You can not use JSON engine of LIBNAME?
bashique
Calcite | Level 5
I have used JSON libname but the problem i am facing is when i have a large json string under "Response" key, only 32767 characters are getting stored in the variable and hence unable to parse
Ksharp
Super User

Also you could use data step to parse JSON file.

 

 

data have;
infile 'c:\temp\have.json' recfm=n dlm='{[,]}';
input have : $2000.;
run;
AllanBowe
Barite | Level 11

The following macro will give you the tools to load a JSON file into a LUA table.  Variables in LUA have no max-length (limited only by memory) so you can perform any processing you need there (using proc lua) before dropping back to SAS.

https://core.sasjs.io/ml__json_8sas_source.html

/Allan
MacroCore library for app developers
Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
bashique
Calcite | Level 5
Hi AllanBowe, can you please specify the usage of LUA in ternm of sas code?
AllanBowe
Barite | Level 11

Sure, here you go:

 

filename fref1 "/path/to/json.file";

/* compile the lua JSON module */
%ml_json()

/* create LUA code  */
data _null_;
  file "/path/to/file.lua";
  put '
    infile = io.open (sas.symget("fpath1"), "r")
    io.input(infile)
    local resp=json.decode(io.read())
    -- grab an element from the LUA table
    local logloc=resp["logLocation"]
    io.close(infile)
  ';
run;

/* execute LUA */
%inc "/path/to/file.lua";

 

/Allan
MacroCore library for app developers
Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1607 views
  • 2 likes
  • 3 in conversation