Yes, basically. There was another section at the top, I wasn't initially coding for, because it wasn't included with the sample code, but it's technically in the requested output. See new requested output format below. { "commRequest": [{ "source": "ppx", "sourceTrnsReferenceId": "long_guid_id1", "requesterId": "svc-ppx", "templateId": "PPX6", "tenateId": 1, "sourceCompanyCode": 1, "Source_ID": { "contract": "ABC123", "partyId": 56789, "language": "en", "Role": "MEMBER", "data": "GEN" }, "deliveryInfo": { "firstName": "JOHN", "middleInitial": "", "lastName": "DOE", "emailAddress": "", "Override": "N", "Channel": "EMAIL" }, "email": { "product": "P", "plan": "Sample", "Type": "A", "unqId": "123456", "grpNb": "XXX12", "divisionNb": "ABC" } }, { "source": "ppx", "sourceTrnsReferenceId": "long_guid_id2", "requesterId": "svc-ppx", "templateId": "PPX6", "tenateId": 1, "sourceCompanyCode": 1, "Source_ID": { "contract": "DEF456", "partyId": 123456, "language": "en", "Role": "MEMBER", "data": "GEN" }, "deliveryInfo": { "firstName": "JANE", "middleInitial": "", "lastName": "DOE", "emailAddress": "", "Override": "N", "Channel": "EMAIL" }, "email": { "product": "H", "plan": "Sample2", "Type": "B", "unqId": "789012", "grpNb": "YYY34", "divisionNb": "DEF" } } I found code that uses a macro and it gets the order right, but the brackets don't look right and I had to include the key(unqid) on each container. So closer but still not perfect. %macro TargetstoJSON(UNQID);
write open object;
write values "commRequest";
/*write open array;*/
export work.commrequest (where=(UNQID=&UNQID) );
write values "Source_ID";
write open array; /* container for member */
export work.member (where=(UNQID=&UNQID) );
write values "delivery_info";
write open array; /* container for delivery */
export work.delivery (where=(UNQID=&UNQID));
write values "email";
write open array; /* container for additional */
export work.additional (where=(UNQID=&UNQID));
write close; /* container for additional */
write close; /* container for the delivery */
write close; /* container for the member */
write close; /* container for commrequest */
%mend TargetstoJSON;
/* Loop through all the consumer ids */
%macro TargetRecords;
%do i = 1 %to &dim_IDs;
%TargetstoJSON(&&&UNQID_&i);
%end;
%mend TargetRecords;
/*
Produce a macro variable for each consumer ID and the
total count of consumerids in the data set.
*/
proc sql;
select UNQID into :UNQID_1 - :UNQID_&SysMaxLong from work.member;
%let dim_IDs = &sqlObs;
quit;
/*
JSON procedure code for getting all the members and their delivery and additional information.
The resultant JSON output is stored in the JSON_TEST2.json file
in the current location.
*/
proc json out="C:\Users\d3jq\Desktop\JSON_TEST2.json" pretty nosastags;
write open array; /* container for all the data */
%TargetRecords;
write close; /* container for all the data */
run; [ { "commRequest": { "source": "ppx", "sourceTrnsReferenceId": "bfe9961i", "requesterId": "svc-ppx", "templateId": "PPX6", "tenateId": 1, "sourceCompanyCode": 1, "UNQID": 123456 }, "Source_ID": [ { "Contract": "ABC123", "PartyID": 567890, "Language": "EN", "Role": "MEMBER", "Data": "GEN", "UNQID": 123456 }, "delivery_info", [ { "FirstName": "JOHN", "MiddleInitial": "", "LastName": "DOE", "EmailAddress": "", "Override": "N", "Channel": "EMAIL", "UNQID": 123456 }, "email", [ { "Product": "P", "Plan": "Sample", "Type": "A", "UNQID": 123456, "GRPNB": "XXX12", "DIVISIONNB": "ABC" } ] ] ] }, { "commRequest": { "source": "ppx", "sourceTrnsReferenceId": "zgtewwyr", "requesterId": "svc-ppx", "templateId": "PPX6", "tenateId": 1, "sourceCompanyCode": 1, "UNQID": 789012 }, "Source_ID": [ { "Contract": "DEF456", "PartyID": 123456, "Language": "EN", "Role": "MEMBER", "Data": "GEN", "UNQID": 789012 }, "delivery_info", [ { "FirstName": "JANE", "MiddleInitial": "", "LastName": "DOE", "EmailAddress": "", "Override": "N", "Channel": "EMAIL", "UNQID": 789012 }, "email", [ { "Product": "H", "Plan": "Sample2", "Type": "B", "UNQID": 789012, "GRPNB": "YYY34", "DIVISIONNB": "DEF" } ] ] ] } ]
... View more