data _null_;
	file _dataout encoding="utf-8";
	length uri $256;
	length line $512;
	length pre $3;
	length ref $16;
	length id $32;
	length desc $200;
	length libraryname $60;
	length assignmode $256;
	length extname $256;
	length extvalue $256;
	length ServerContext $256;
	length server $32;
	length uri2 $256;
	nobj=1;
	n=1;
	objrc=1;
	n2=1;
	ServerContext="";
	uri="";
	id="";
	line="";
	metaserver=getoption("METASERVER");

	put "{";
	put '"libraries" : [';

	if (metaserver ne '') then do;
	
		do while(nobj >= 0);

			nobj=metadata_getnobj("omsobj:SASLibrary?@IsPreassigned = '0'",n,uri);

			if (nobj>0) then do;
				rc=metadata_getattr(uri,"id",id);
				rc=metadata_getattr(uri,"Libref",ref);
				rc=metadata_getattr(uri,"IsPreassigned",pre);				
				rc=metadata_getattr(uri, "Desc", desc);
				desc = tranwrd(desc, '"', '\"');  /*double quotes need JSON escape*/
				desc = prxchange("s/\n/\\n/", -1, desc); /* so do newlines */

				rc=metadata_getattr(uri, "Name", libraryname);
				libraryname = tranwrd(libraryname, '"', '\"');  /*double quotes need JSON escape*/
 	   	
				/* The METADATA_GETNASN function gets objects associated
					via the DeployedComponents association. */
				n2=1;
				objrc=metadata_getnasn(uri,"DeployedComponents",n2,uri2);
				if objrc<=0 then
				do;
					/* NOTE: There is no DeployedComponents association and therefore no server context. */
					ServerContext="";
				end;

				/* When an association is found, the METADATA_GETATTR function gets the server name. */
				do while(objrc>0);
					objrc=metadata_getattr(uri2,"Name",server);
					server = tranwrd(server, '"', '\"');  /*double quotes need JSON escape*/
					if n2=1 then ServerContext="'"||trim(server)||"'";
					else ServerContext=trim(ServerContext)||",'"||trim(server)||"'";

					/* Look for another ServerContext */
					n2+1;
					objrc=metadata_getnasn(uri,"DeployedComponents",n2,uri2);
				end; /*do while objrc*/ 
	
				/* AssignMode is an attribute of the Extensions associated object */
				n2 = 1;
				assignmode = "";

				do until(objrc <= 0);			
					objrc = metadata_getnasn(uri, "Extensions" , n2 , uri2);
					if objrc > 0 then
					do;
						objrc = metadata_getattr(uri2, "Name", extname);
						objrc = metadata_getattr(uri2, "Value", extvalue);
						if objrc = 0 and extname = "AssignMode" then assignmode = extvalue;
					end;
					n2 = n2 + 1;
				end; /* until(objrc <= 0) */

				line=cat('{"id":"',trim(id),'","name":"',upcase(trim(ref)),'","preassigned":',
							trim(pre),',"server":"',trim(ServerContext),'","desc":"',trim(desc),'","libraryname":"',upcase(trim(libraryname)),'","assignmode":"',trim(assignmode),'"}');
				put line;
				if (n ne nobj) then do;
					put ",";
 				end;

			end; /* if (nobj>0) */

			n=n+1;
		end; /* while(nobj >= 0) */
	end; /* if (metaserver ne '') */
	
	put "]";
	put "}";
run;

%let _DATAOUT_MIME_TYPE=text/plain;
%let _DATAOUT_NAME=metalibs.txt; 