<?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: How to parse JSON array and store values in SAS Column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-JSON-array-and-store-values-in-SAS-Column/m-p/784303#M250267</link>
    <description>&lt;P&gt;The JSON libname engine is the best way to read JSON-formatted content as data within SAS. However, the JSON engine has two requirements:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The data must be complete, well-formed JSON.&lt;/LI&gt;
&lt;LI&gt;The source must be referenced as a fileref, not as a character variable or string literal or any "in-memory" SAS construct.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;What this means for you: to read a data set where each row contains complete JSON content, you must run a first pass to extract all of the data and assemble into a larger JSON structure that SAS can read all at once. (Alternatively, you could read &lt;STRONG&gt;each&lt;/STRONG&gt; row and write to a standalone JSON file, then use the JSON libname to read that file -- repeating for &lt;STRONG&gt;each&lt;/STRONG&gt; row. Tedious, but for large JSON chunks you might find it useful.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example -- using your example data -- for assembling a larger JSON file and then using SAS to read it as a single data source.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data have;
infile cards truncover;
input ID Data $200.;
cards;
1  {"name":"John Peter","email":"john.peter@outlook.com", "attachments":[]}
2 {"name":"Mary White","email":"mary1111@outlook.com", "attachments":[{"documentId":"a284h162","name":"AlainDLback.jpg","result":{"status":"approve","time":"2021-03-05"}}]}
;

filename alljson temp;
data _null_;
 set have end=done;
 file alljson;
 if _n_=1 then put '[{"records":['; /* outer wrapper for JSON */
 put data;
 if done=1 then put ']}]'; /* close outer wrapper */
   else put ','; /* each 'record' must be separated by a comma */
run;

libname data json fileref=alljson;

/* show the tables in the resulting library */
proc datasets lib=data; quit;

/* example of pulling out one table */
data records;
 set data.records;
run;

title "Records data";
proc print data=records; run;

title "Records and their attachments";
proc sql;
 select t1.name, t1.email, t2.documentId, t2.name as docName
 from data.records t1 left join data.records_attachments t2
 on (t1.ordinal_records = t2.ordinal_records);
quit;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The resulting library is like a relational database with tables that you can join to create the output you need, as shown in the PROC SQL code in the example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ChrisHemedinger_0-1638795593857.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66435i9DD24F88A9FF0B5A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ChrisHemedinger_0-1638795593857.png" alt="ChrisHemedinger_0-1638795593857.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 06 Dec 2021 17:59:03 GMT</pubDate>
    <dc:creator>ChrisHemedinger</dc:creator>
    <dc:date>2021-12-06T17:59:03Z</dc:date>
    <item>
      <title>How to parse JSON array and store values in SAS Column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-JSON-array-and-store-values-in-SAS-Column/m-p/784233#M250234</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to work with a JSON text string column from a SAS dataset. Below is sample records of this column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Data&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;{"name":"John Peter","email":"&lt;A href="mailto:john.peter@outlook.com&amp;quot;," target="_blank" rel="noopener"&gt;john.peter@outlook.com",&lt;/A&gt; "attachments":[]}&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;{"name":"Mary White","email":"mary1111@outlook.com", "attachments":[{"documentId":"a284h162","name":"AlainDLback.jpg","result":{"status":"approve","time":"2021-03-05"}}]}&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Desired output is to transform the dataset to the below form.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;name&lt;/TD&gt;&lt;TD&gt;email&lt;/TD&gt;&lt;TD&gt;attachments_documentId&lt;/TD&gt;&lt;TD&gt;attachments_name&lt;/TD&gt;&lt;TD&gt;attachments_result_status&lt;/TD&gt;&lt;TD&gt;attachments_result_time&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;John Peter&lt;/TD&gt;&lt;TD&gt;&lt;A href="mailto:john.peter@outlook.com&amp;quot;," target="_blank" rel="noopener"&gt;john.peter@outlook.com&lt;/A&gt;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Mary White&lt;/TD&gt;&lt;TD&gt;mary1111@outlook.com&lt;/TD&gt;&lt;TD&gt;a284h162&lt;/TD&gt;&lt;TD&gt;AlainDLback.jpg&lt;/TD&gt;&lt;TD&gt;approve&lt;/TD&gt;&lt;TD&gt;2021-03-05&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Any pointers on the approach to follow would be very helpful. Thank you!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Dec 2021 03:06:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-JSON-array-and-store-values-in-SAS-Column/m-p/784233#M250234</guid>
      <dc:creator>newboy1218</dc:creator>
      <dc:date>2021-12-06T03:06:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse JSON array and store values in SAS Column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-JSON-array-and-store-values-in-SAS-Column/m-p/784276#M250248</link>
      <description>&lt;P&gt;Take a look at the &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsglobal/n1jfdetszx99ban1rl4zll6tej7j.htm#n0o4g5of5a0g1vn1ci5mwrykodte" target="_self"&gt;JSON engine&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;There's a detailed example below the heading "Creating and Editing a JSON MAP"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Dec 2021 11:19:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-JSON-array-and-store-values-in-SAS-Column/m-p/784276#M250248</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2021-12-06T11:19:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse JSON array and store values in SAS Column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-JSON-array-and-store-values-in-SAS-Column/m-p/784287#M250255</link>
      <description>&lt;PRE&gt;data have;
infile cards truncover;
input ID Data $200.;
cards;
1  {"name":"John Peter","email":"john.peter@outlook.com", "attachments":[]}
2 {"name":"Mary White","email":"mary1111@outlook.com", "attachments":[{"documentId":"a284h162","name":"AlainDLback.jpg","result":{"status":"approve","time":"2021-03-05"}}]}
;


data temp;
 set have;
 do i=1 to countw(data,'{[]},','q');
  temp=scan(data,i,'{[]},','q');
  vname=dequote(strip(scan(temp,1,':')));
  value=dequote(strip(scan(temp,-1,':')));
  output;
 end;
 keep id vname value;
run;

data temp1;
 set temp;
 by id;
 retain found;
 if first.id then found=0;
 if missing(vname) then delete;
 if find(vname,'attachments','i') then do; found=1;delete;end;
 if found then vname=cats('attachments','_',vname);
run;

proc transpose data=temp1 out=want(drop=_:) ;
by id ;
var value;
id vname;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Dec 2021 11:56:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-JSON-array-and-store-values-in-SAS-Column/m-p/784287#M250255</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-12-06T11:56:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse JSON array and store values in SAS Column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-JSON-array-and-store-values-in-SAS-Column/m-p/784303#M250267</link>
      <description>&lt;P&gt;The JSON libname engine is the best way to read JSON-formatted content as data within SAS. However, the JSON engine has two requirements:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The data must be complete, well-formed JSON.&lt;/LI&gt;
&lt;LI&gt;The source must be referenced as a fileref, not as a character variable or string literal or any "in-memory" SAS construct.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;What this means for you: to read a data set where each row contains complete JSON content, you must run a first pass to extract all of the data and assemble into a larger JSON structure that SAS can read all at once. (Alternatively, you could read &lt;STRONG&gt;each&lt;/STRONG&gt; row and write to a standalone JSON file, then use the JSON libname to read that file -- repeating for &lt;STRONG&gt;each&lt;/STRONG&gt; row. Tedious, but for large JSON chunks you might find it useful.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example -- using your example data -- for assembling a larger JSON file and then using SAS to read it as a single data source.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data have;
infile cards truncover;
input ID Data $200.;
cards;
1  {"name":"John Peter","email":"john.peter@outlook.com", "attachments":[]}
2 {"name":"Mary White","email":"mary1111@outlook.com", "attachments":[{"documentId":"a284h162","name":"AlainDLback.jpg","result":{"status":"approve","time":"2021-03-05"}}]}
;

filename alljson temp;
data _null_;
 set have end=done;
 file alljson;
 if _n_=1 then put '[{"records":['; /* outer wrapper for JSON */
 put data;
 if done=1 then put ']}]'; /* close outer wrapper */
   else put ','; /* each 'record' must be separated by a comma */
run;

libname data json fileref=alljson;

/* show the tables in the resulting library */
proc datasets lib=data; quit;

/* example of pulling out one table */
data records;
 set data.records;
run;

title "Records data";
proc print data=records; run;

title "Records and their attachments";
proc sql;
 select t1.name, t1.email, t2.documentId, t2.name as docName
 from data.records t1 left join data.records_attachments t2
 on (t1.ordinal_records = t2.ordinal_records);
quit;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The resulting library is like a relational database with tables that you can join to create the output you need, as shown in the PROC SQL code in the example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ChrisHemedinger_0-1638795593857.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66435i9DD24F88A9FF0B5A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ChrisHemedinger_0-1638795593857.png" alt="ChrisHemedinger_0-1638795593857.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Dec 2021 17:59:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-JSON-array-and-store-values-in-SAS-Column/m-p/784303#M250267</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2021-12-06T17:59:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse JSON array and store values in SAS Column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-JSON-array-and-store-values-in-SAS-Column/m-p/785381#M250644</link>
      <description>&lt;P&gt;One rather elegant way to import JSON into SAS is to use PROC LUA.&amp;nbsp; Lua can handle arrays, very long strings, nested data structures etc with ease.&amp;nbsp; It also has all necessary plumbing to write that data straight back into SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To save time, we've compiled an open source Lua JSON parser into a SAS Macro here:&amp;nbsp;&lt;A href="https://core.sasjs.io/ml__json_8sas.html" target="_blank"&gt;https://core.sasjs.io/ml__json_8sas.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;An example of this being used is in this macro, which fetches the code for a SAS job from the Viya APIs:&amp;nbsp; &lt;A href="https://core.sasjs.io/mv__getjobcode_8sas.html" target="_blank"&gt;https://core.sasjs.io/mv__getjobcode_8sas.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Dec 2021 13:05:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-JSON-array-and-store-values-in-SAS-Column/m-p/785381#M250644</guid>
      <dc:creator>AllanBowe</dc:creator>
      <dc:date>2021-12-10T13:05:36Z</dc:date>
    </item>
  </channel>
</rss>

