<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Exporting Nested Tables to JSON in Developers</title>
    <link>https://communities.sas.com/t5/Developers/Exporting-Nested-Tables-to-JSON/m-p/717112#M1122</link>
    <description>&lt;P&gt;The PROC JSON EXPORT statement puts each observation of the data set data into a JSON container. Whether the containers are JSON objects or JSON arrays depends on the JSON KEYS argument. Specifying the KEYS argument outputs each observation in a JSON object and NOKEYS outputs to JSON arrays. Your desired JSON shows sending the observations to an already open JSON object. As you have seen, you will not be able to do that with an EXPORT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To use PROC JSON and get the desired output, I would recommend using a DATA Step to create the PROC JSON code and then run the generated program. The advantage of using generated PROC JSON code is that that PROC JSON handles producing correct JSON syntax.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the SAS program that will produce the desired output based on your supplied JSON file having been read in via the JSON LIBNAME.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*****************************
* Modifiable macro variables
******************************/
%let jsonProcCodeSpec=sasuser\jsonProcCode.sas;
%let jsonOutputSpec=sasuser\jsonOutput.txt;

%put &amp;amp;jsonProcCodeSpec;
/*****************************
* Constant macro variables
******************************/
%let stmtEnd=%STR(;);

/* Write the custom PROC JSON code based on the values in the data set. 
   This will be run after the DATA Step creates it. */
data _null_;
  /* specifies the output file for PUT statements                      */
  FILE "&amp;amp;jsonProcCodeSpec" DISK;
  set x.owner(rename=(name=owner_name)) end=last; 

  if _N_ eq 1 
    then do;
      /* Only on the first observation in the data set, write the required 
         initial statements to the JSON procedure code file.                */
      put "proc json pretty out=""&amp;amp;jsonOutputSpec"" nosastags &amp;amp;stmtEnd";
      put "    write open object &amp;amp;stmtEnd";
	  put "      write values ""Owner"" &amp;amp;stmtEnd";
	  put "        write open array &amp;amp;stmtEnd";
	  put "            write open object &amp;amp;stmtEnd";
	  put "              write values ""Cars"" &amp;amp;stmtEnd";
	  put "                write open array &amp;amp;stmtEnd";
	  put "                    export x.owner_cars(drop=ordinal_:) &amp;amp;stmtEnd";
	  put "                write close &amp;amp;stmtEnd";
	  end;
  /* Write the data set Name column to JSON */
  put "write value ""Name"" """owner_Name""" &amp;amp;stmtEnd";
  /* Write the data set Age column to JSON */
  put "write value ""Age"" " age "&amp;amp;stmtEnd";
  if last
    then do;
      /* Only on the last observation in the data set, write the required 
         final statements to the JSON procedure code file.                */
	  put "            write close &amp;amp;stmtEnd";
	  put "        write close &amp;amp;stmtEnd";
	  put "    write close &amp;amp;stmtEnd";
      put "run &amp;amp;stmtEnd";
    end;
run;

/* Now run the generated custom JSON procedure code to produce the 
   JSON formatted output file of the data set.                         */
%include "&amp;amp;jsonProcCodeSpec";
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 05 Feb 2021 16:33:13 GMT</pubDate>
    <dc:creator>BillM_SAS</dc:creator>
    <dc:date>2021-02-05T16:33:13Z</dc:date>
    <item>
      <title>Exporting Nested Tables to JSON</title>
      <link>https://communities.sas.com/t5/Developers/Exporting-Nested-Tables-to-JSON/m-p/717043#M1120</link>
      <description>&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;Hi all,&lt;/P&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;I'm having a little trouble exporting data to JSON. Here's the data I'm trying to export, in the structure that I'm trying to replicate:&lt;/P&gt;&lt;PRE class="_3GnarIQX9tD_qsgXkfSDz1"&gt;&lt;CODE class="_34q3PgLsx9zIU5BiSOjFoM"&gt;{
    "Owner": [
        {
            "Cars": [
                {
                    "Make":"Manufacturer 1"
                },
                {
                    "Make":"Manufacturer 2"
                }
            ],
            "Name":"SAS Man",
            "Age":"20"
        }
    ]
}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;I can read that into SAS with a JSON libname and copied into work no problem. This is the form of the SAS tables that I'm trying to export. The problem is trying to export it back into the same structure. Here's the code I have at the moment:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc json out=outjson pretty nosastags;
    write open object; write values "Owner";
        write open array; /* I know this is superfluous. I forgot to take it out for the MWE :S */
            write open object; write values "Cars";
                write open array;
                    export work.owner_cars(drop=ordinal_:);
                write close;
                write values "";
                export work.owner(drop=ordinal_:);
            write close;
        write close;
    write close;
run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;The problem is that this ends up with Owner being an object containing the cars array (good), but with a separate dummy object containing the other data in the owner table (bad):&lt;/P&gt;&lt;PRE class="_3GnarIQX9tD_qsgXkfSDz1"&gt;&lt;CODE class="_34q3PgLsx9zIU5BiSOjFoM"&gt;{
    "Owner": [
        {
            "Cars": [
                {
                    "Make":"Manufacturer 1"
                },
                {
                    "Make":"Manufacturer 2"
                }
            ],
            "": {
                "Name":"SAS Man",
                "Age":"20"
            }
        }
    ]
}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;Is there any way I can nest the work.owner_cars data inside the owner table? Perhaps using the map or alldata that was created as part of the JSON libname?&lt;/P&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Feb 2021 11:23:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Exporting-Nested-Tables-to-JSON/m-p/717043#M1120</guid>
      <dc:creator>sc31415</dc:creator>
      <dc:date>2021-02-05T11:23:43Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting Nested Tables to JSON</title>
      <link>https://communities.sas.com/t5/Developers/Exporting-Nested-Tables-to-JSON/m-p/717107#M1121</link>
      <description>&lt;P&gt;Just write the JSON yourself.&amp;nbsp; Here is how to do it for your example.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options parmcards=json;
filename json temp;
parmcards4;
{
    "Owner": [
        {
            "Cars": [
                {
                    "Make":"Manufacturer 1"
                },
                {
                    "Make":"Manufacturer 2"
                }
            ],
            "Name":"SAS Man",
            "Age":"20"
        }
    ]
}
;;;;

libname json json ;

data for_print;
  merge json.owner json.owner_cars end=eof;
  by ordinal_owner ;
  file json2 ;
  if _n_=1 then put '{"Owner":' / ' [' ;
  if first.ordinal_owner then put
      '  {"Name": ' name :$quote.
    / '  ,"Age": ' age :$quote.
    / '  ,"Cars":'
    / '   [' @
  ;
  else put '   ,' @ ;
  put '{"Make": ' make $quote. '}' ;
  if last.ordinal_owner then put '   ]' / '  }' ;
  if eof then put ' ]' / '}' ;
run;

libname json2 json;
proc compare data=json.owner compare=json2.owner; run;
proc compare data=json.owner_cars compare=json2.owner_cars; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is the generated JSON2 file:&lt;/P&gt;
&lt;PRE&gt;{"Owner":
 [
  {"Name": "SAS Man"
  ,"Age": "20"
  ,"Cars":
   [{"Make": "Manufacturer 1"}
   ,{"Make": "Manufacturer 2"}
   ]
  }
 ]
}&lt;/PRE&gt;
&lt;P&gt;Note this will be harder if there is more than one sub list (the CARS list in this example) you want to generate.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Feb 2021 16:39:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Exporting-Nested-Tables-to-JSON/m-p/717107#M1121</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-05T16:39:03Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting Nested Tables to JSON</title>
      <link>https://communities.sas.com/t5/Developers/Exporting-Nested-Tables-to-JSON/m-p/717112#M1122</link>
      <description>&lt;P&gt;The PROC JSON EXPORT statement puts each observation of the data set data into a JSON container. Whether the containers are JSON objects or JSON arrays depends on the JSON KEYS argument. Specifying the KEYS argument outputs each observation in a JSON object and NOKEYS outputs to JSON arrays. Your desired JSON shows sending the observations to an already open JSON object. As you have seen, you will not be able to do that with an EXPORT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To use PROC JSON and get the desired output, I would recommend using a DATA Step to create the PROC JSON code and then run the generated program. The advantage of using generated PROC JSON code is that that PROC JSON handles producing correct JSON syntax.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the SAS program that will produce the desired output based on your supplied JSON file having been read in via the JSON LIBNAME.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*****************************
* Modifiable macro variables
******************************/
%let jsonProcCodeSpec=sasuser\jsonProcCode.sas;
%let jsonOutputSpec=sasuser\jsonOutput.txt;

%put &amp;amp;jsonProcCodeSpec;
/*****************************
* Constant macro variables
******************************/
%let stmtEnd=%STR(;);

/* Write the custom PROC JSON code based on the values in the data set. 
   This will be run after the DATA Step creates it. */
data _null_;
  /* specifies the output file for PUT statements                      */
  FILE "&amp;amp;jsonProcCodeSpec" DISK;
  set x.owner(rename=(name=owner_name)) end=last; 

  if _N_ eq 1 
    then do;
      /* Only on the first observation in the data set, write the required 
         initial statements to the JSON procedure code file.                */
      put "proc json pretty out=""&amp;amp;jsonOutputSpec"" nosastags &amp;amp;stmtEnd";
      put "    write open object &amp;amp;stmtEnd";
	  put "      write values ""Owner"" &amp;amp;stmtEnd";
	  put "        write open array &amp;amp;stmtEnd";
	  put "            write open object &amp;amp;stmtEnd";
	  put "              write values ""Cars"" &amp;amp;stmtEnd";
	  put "                write open array &amp;amp;stmtEnd";
	  put "                    export x.owner_cars(drop=ordinal_:) &amp;amp;stmtEnd";
	  put "                write close &amp;amp;stmtEnd";
	  end;
  /* Write the data set Name column to JSON */
  put "write value ""Name"" """owner_Name""" &amp;amp;stmtEnd";
  /* Write the data set Age column to JSON */
  put "write value ""Age"" " age "&amp;amp;stmtEnd";
  if last
    then do;
      /* Only on the last observation in the data set, write the required 
         final statements to the JSON procedure code file.                */
	  put "            write close &amp;amp;stmtEnd";
	  put "        write close &amp;amp;stmtEnd";
	  put "    write close &amp;amp;stmtEnd";
      put "run &amp;amp;stmtEnd";
    end;
run;

/* Now run the generated custom JSON procedure code to produce the 
   JSON formatted output file of the data set.                         */
%include "&amp;amp;jsonProcCodeSpec";
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Feb 2021 16:33:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Exporting-Nested-Tables-to-JSON/m-p/717112#M1122</guid>
      <dc:creator>BillM_SAS</dc:creator>
      <dc:date>2021-02-05T16:33:13Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting Nested Tables to JSON</title>
      <link>https://communities.sas.com/t5/Developers/Exporting-Nested-Tables-to-JSON/m-p/717152#M1123</link>
      <description>&lt;P&gt;How could that work where there is more than one OWNER?&lt;/P&gt;</description>
      <pubDate>Fri, 05 Feb 2021 17:40:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Exporting-Nested-Tables-to-JSON/m-p/717152#M1123</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-05T17:40:13Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting Nested Tables to JSON</title>
      <link>https://communities.sas.com/t5/Developers/Exporting-Nested-Tables-to-JSON/m-p/717158#M1124</link>
      <description>&lt;P&gt;I produced what was asked. I hope that the technique I showed gives the customer a starting point from which to modify this most-likely simple case to whatever data the customer has.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Feb 2021 17:52:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Exporting-Nested-Tables-to-JSON/m-p/717158#M1124</guid>
      <dc:creator>BillM_SAS</dc:creator>
      <dc:date>2021-02-05T17:52:23Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting Nested Tables to JSON</title>
      <link>https://communities.sas.com/t5/Developers/Exporting-Nested-Tables-to-JSON/m-p/717161#M1125</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/3480"&gt;@BillM_SAS&lt;/a&gt; for your solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;+1 to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; . I forgot to say that there will be more than one owner.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll have a play with your solutions when I get back in to the office on Monday. Have a good weekend!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Feb 2021 17:54:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Exporting-Nested-Tables-to-JSON/m-p/717161#M1125</guid>
      <dc:creator>sc31415</dc:creator>
      <dc:date>2021-02-05T17:54:42Z</dc:date>
    </item>
  </channel>
</rss>

