BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
thesasuser
Lapis Lazuli | Level 10

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"
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

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;
thesasuser
Lapis Lazuli | Level 10

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;

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
  • 2 replies
  • 396 views
  • 0 likes
  • 2 in conversation