DATA Step, Macro, Functions and more

JSON Libname

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

JSON Libname

Hello,

 

Why Am I having this error on my JSON libname:

 

ERROR: Invalid JSON in input near line 1 column 1: Encountered an illegal character.

ERROR: Error in the LIBNAME statement.

 

I used the following code:

 

proc json out='C:\Users\GARCIAAV\Dropbox\JSON\test.json' pretty;

export sashelp.class (where=(age=12));

run;

filename help temp;

/* Neat service from Open Notify project */

proc http

/* url="http://api.open-notify.org/astros.json"*/

url="https://www.dropbox.com/home/JSON?preview=test.json"

method= "GET"

out=help;

run;

/* Assign a JSON library to the HTTP response */

libname test JSON fileref=help;

 

Thanks,

 

Ave


Accepted Solutions
Solution
‎06-28-2017 08:06 AM
Community Manager
Posts: 2,757

Re: JSON Libname

If you have the JSON file locally, you simply assign a SAS fileref with the FILENAME statement to that location.  No need to jump through the PROC HTTP hoops.  That's only for retrieving JSON data from a web service.  

 

Here's an example.  Adjust the file paths for your system.

 

/* Create a JSON file */
proc json out="c:\temp\test.json" pretty;
 export sashelp.class (where=(age=12));
run;

/* Reference the JSON file and read it in using JSON libname */
filename testjs "c:\temp\test.json";
libname test JSON fileref=testjs;

proc datasets lib=test;
quit;

proc print data=test.sastabledata_class;
run;

View solution in original post


All Replies
Community Manager
Posts: 2,757

Re: JSON Libname

If I understand your test correctly, you're testing a "round trip" of PROC JSON to the JSON libname.  By reading the result from Dropbox, you'll need to provide a URL to the "raw" view of the Dropbox file.  The link you're using goes to a web page that presents a preview of the file, but does not serve up the file itself.

 

See my blog post about using SAS to read from Dropbox.  You'll have to get the "direct file" link from your Dropbox view.

Occasional Contributor
Posts: 6

Re: JSON Libname

Thank you for your reply and I apologize for if I am not clear.

 

What I really want to do is to import JSON file to SAS using JSON libname. If I have my JSON file in my local directory, what will be the syntax that I need to used?

Solution
‎06-28-2017 08:06 AM
Community Manager
Posts: 2,757

Re: JSON Libname

If you have the JSON file locally, you simply assign a SAS fileref with the FILENAME statement to that location.  No need to jump through the PROC HTTP hoops.  That's only for retrieving JSON data from a web service.  

 

Here's an example.  Adjust the file paths for your system.

 

/* Create a JSON file */
proc json out="c:\temp\test.json" pretty;
 export sashelp.class (where=(age=12));
run;

/* Reference the JSON file and read it in using JSON libname */
filename testjs "c:\temp\test.json";
libname test JSON fileref=testjs;

proc datasets lib=test;
quit;

proc print data=test.sastabledata_class;
run;
Occasional Contributor
Posts: 6

Re: JSON Libname

this perfectly worked! thank you so much!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 174 views
  • 1 like
  • 2 in conversation