Importing one line json file into SAS
I have data in the following json format:
{"metadata1":"val1","metadata2":"val2","data_rows":[{"var1":1,"var2":2,"var3":3},{"var1":4,"var2":5,"var3":6}]}
At the beginning there are several metadata variables that appear only once, and then several data records, all on the same line. How do I import this into a SAS dataset?
+3
source to share
1 answer
/*Create json file containing sample data*/
filename json "%sysfunc(pathname(work))\json.txt";
data _null_;
file json;
put '{"metadata1":"val1,","metadata2":"val2}","data_rows":[{"var1":1,"var2":2,"var3":3},{"var1":4,"var2":5,"var3":6}]}';
run;
/*Data step for importing the json file*/
data want;
infile json dsd dlm='},' lrecl = 1000000 n=1;
retain metadata1 metadata2;
if _n_ = 1 then input @'metadata1":' metadata1 :$8. @'metadata2":' metadata2 :$8. @;
input @'var1":' var1 :8. @'var2":' var2 :8. @'var3":' var3 :8. @@;
run;
Notes:
- The SAS point to start reading each variable is set using logic
@'string'
. - Setting
,
both}
delimiters and using format modifiers:
in an input statement instructs SAS to continue reading characters from the specified starting point until the maximum requested number or delimiter is read. - The setting
dsd
in the infile statement removes double quotes from character data values ββand prevents any problems from occurring if the character variables contain delimiters. - The double trailing
@
instructs SAS to continue reading more records from the same line using the same logic until it reaches the end of the line. - Metadata variables are treated as a special case using a separate input statement. If necessary, they can be redirected in one line to a separate file.
-
lrecl
must be greater than or equal to the length of your file for this approach to work. - The setting
n=1
should help reduce memory usage if your file is very large, preventing SAS from trying to buffer multiple lines of input.
+3
source to share