Hello
I was trying to run the code from the paper Creating and Controlling JSON Output with PROC JSON page 8 and getting an error which I am not able to comprehend.
The error is "ERROR: A string is required at this point in the text so that an object key may be emitted."
I am reproducing the log with code below.
Wondering if some one can help?
28 proc json out="nested_output.json" pretty nosastags ; 29 write open array; 30 write open object; 31 write values "Title" "Employees"; 32 write values "Description" "Information about employees about the company"; 33 34 write open array ; ERROR: A string is required at this point in the text so that an object key may be emitted. 35 export employees / fmtdatetime; 36 write close; 37 38 write close; 39 40 write close ; 41 run; ERROR: Due to the previous error, the JSON output file is incomplete and invalid, or in some cases, unable to be created. If created, the JSON output file is retained so that you can review it to help determine the cause of the error. ...... NOTE: The SAS System stopped processing this step because of errors.
The content of the json file are as follows
[ { "Title": "Employees", "Description": "Information about employees about the company"
Since you are in the middle or writing an OBJECT you need write the tag name for the array object first.
Try this example.
filename json temp;
proc json out=json;
write open array;
write open object;
write values 'Title' 'Class';
write values 'Description' 'Information about students';
write values 'Class' ;
write open array;
export sashelp.class(obs=2 keep=name sex age) / nosastags ;
write close ;
write close ;
write close ;
run;
data _null_;
infile json;
input; put _infile_;
run;
libname json json ;
proc copy inlib=json outlib=work;
run;
proc print data=class;
run;
Since you are in the middle or writing an OBJECT you need write the tag name for the array object first.
Try this example.
filename json temp;
proc json out=json;
write open array;
write open object;
write values 'Title' 'Class';
write values 'Description' 'Information about students';
write values 'Class' ;
write open array;
export sashelp.class(obs=2 keep=name sex age) / nosastags ;
write close ;
write close ;
write close ;
run;
data _null_;
infile json;
input; put _infile_;
run;
libname json json ;
proc copy inlib=json outlib=work;
run;
proc print data=class;
run;
Thanks @Tom .
Following your example I modified the original code and it also works.
Thanks once again.
The update code is here for reference.
proc json out="nested_output.json" pretty nosastags ;
write open array;
write open object;
write values "Title" "Employees";
write values "Description" "Information about employees about the company";
Write values 'Employees';
write open array ;
export work.employees / fmtdatetime;
write close;
write close;
write close ;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.