Export data to JSON without array containers

Reply
Contributor
Posts: 22

Export data to JSON without array containers

I need to post a JSON body through a REST API. The expected body is a JSON object containing some variables and values, nested as one object:

 

    {
    "Description": "Test",
    "FromEmail": "some@email.no",
    "FromName": "somename"
     }

But SAS puts the object into a JSON array container:

 

[
{ "Description": "Test", "FromEmail": "some@email.no", "FromName": "somename" }
]

 

I know I can use some string function to remove the brackets, but the surely must be an option for not putting it into an array? I just can't seem to find it. Have tried using write open and write close, but I cant get rid of them.

Trusted Advisor
Posts: 1,301

Re: Export data to JSON without array containers

What are you currently using to produce the JSON? PROC JSON?
Contributor
Posts: 22

Re: Export data to JSON without array containers

Yes, proc json:

Proc json out="\\myfolder\json.json" pretty;
export jsondata /noSAStags; 
run;
Trusted Advisor
Posts: 1,301

Re: Export data to JSON without array containers

PROC JSON does not really have options to easily modify it's preffered schema for the export statement. This really leaves two viable options for as simple an object as you are trying to produce.

1. You can produce the object using a data step and the put statement, something like the following

data _null_;
set jsondata;
put '{'/"Description": ' description $quote. /',"FromEmail": ' fromemail $quote. /',"FromName": ' fromname $quote. / '}';
run;

2. You can use the WRITE OPEN and WRITE VALUES statements in PROC JSON with macro variables

proc sql;
select description,fromemail,fromname
into :description,:fromemail,:fromname
from jsondata;
quit;

PROC JSON out=file pretty;
WRITE OPEN O;
WRITE VALUES "Description" "&description."
"FromEmail" "&fromemail."
"FromName" "&fromname.";
WRITE CLOSE;
RUN;
Ask a Question
Discussion stats
  • 3 replies
  • 503 views
  • 0 likes
  • 2 in conversation