<?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: Parsing a JSON file in SAS in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531968#M32957</link>
    <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How would I see the map which SAS generates?&lt;/P&gt;</description>
    <pubDate>Fri, 01 Feb 2019 13:37:42 GMT</pubDate>
    <dc:creator>Sean_OConnor</dc:creator>
    <dc:date>2019-02-01T13:37:42Z</dc:date>
    <item>
      <title>Parsing a JSON file in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531076#M32938</link>
      <description>&lt;P&gt;Folks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm new to json formats and I'm having trouble attempting to import data into SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was orginally given a json schema which I could load perfectly into SAS with the following code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename in "\\location\BigSimA1000C100D10.txt";
filename map 'my.map';
libname in_json json map=map automap=reuse;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This data was in a nested format and SAS seemed to parse it perfectly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, the data received yesterday is different.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When opening in notepad++ it looks like one record per line. I'm told this is a valid json format.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A record always begins as such&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;{"time_stamp"&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;and ends as follows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;false}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;There are other curly between the two above but is there an easy way to either parse this json file as done above or can anyway provide me with some information of how I might go about it?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 16:57:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531076#M32938</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2019-01-29T16:57:42Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a JSON file in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531111#M32939</link>
      <description>&lt;P&gt;Very interesting!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is something I wanted to know more about anyway, so I played around with it a bit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You didn't specify the exact format of your JSON text, so I "invented" the following, from a SAS paper that I reviewed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;{"firstName": "John", "lastName": "Smith", "age": 25}&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I run the following code, which creates a one-line file, it works:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let DirRef = /home/me;

data _null_;
length OutLine $32767;
file "&amp;amp;DirRef./JSExample.json" lrecl=32767;
OutLine = '{"firstName": "John", "lastName": "Smith", "age": 25}'; put OutLine;
/* OutLine = '{"firstName": "Joho", "lastName": "Smiti", "age": 26}'; put OutLine; */
run;

filename jsonxmp "&amp;amp;DirRef./JSExample.json";

libname jstest JSON fileref=jsonxmp;

proc print data=jstest.root;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, when I run the following, which creates two lines:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let DirRef = /home/cstkari/my_content;

data _null_;
length OutLine $32767;
file "&amp;amp;DirRef./JSExample.json" lrecl=32767;
OutLine = '{"firstName": "John", "lastName": "Smith", "age": 25}'; put OutLine;
OutLine = '{"firstName": "Joho", "lastName": "Smiti", "age": 26}'; put OutLine;
run;

filename jsonxmp "&amp;amp;DirRef./JSExample.json";

libname jstest JSON fileref=jsonxmp;

proc print data=jstest.root;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;it fails with the following error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt; ERROR: Invalid JSON in input near line 2 column 2: Unexpected characters found after valid JSON text.
 ERROR: Error in the LIBNAME statement.
&lt;/PRE&gt;
&lt;P&gt;Either the multi-line format isn't actually valid JSON, or SAS can't interpret a valid JSON format correctly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps!&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; Tom&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 18:32:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531111#M32939</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2019-01-29T18:32:35Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a JSON file in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531112#M32940</link>
      <description>&lt;P&gt;That is NOT a JSON file.&amp;nbsp; That is something called JSON LInes.&amp;nbsp;&amp;nbsp;&lt;A href="http://jsonlines.org/&amp;nbsp;" target="_blank" rel="noopener"&gt;http://jsonlines.org/&amp;nbsp;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SAS engine does NOT support that format.&lt;/P&gt;
&lt;P&gt;You will need to modify the file to wrap the lines up into a valid JSON list.&amp;nbsp; Or parse the lines one by one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's make a file in that format so we have something to test with.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename json temp;
data _null_;
  file json ;
  put  '{"firstName": "John", "lastName": "Smith", "age": 25}';
  put  '{"firstName": "Joho", "lastName": "Smiti", "age": 26}';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If we try to read it we will get an error.&lt;/P&gt;
&lt;P&gt;But if we convert it to a valid JSON string:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename json2 temp;

data _null_;
  infile json end=eof;
  file json2 ;
  input ;
  if _n_=1 then put '[' @; else put ',' @;
  put _infile_;
  if eof then put ']';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then we can read it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename map2 temp;
libname json2 JSON map=map2 automap=reuse ;

proc print data=json2.root; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;       ordinal_    first    last
Obs      root      Name     Name     age

 1         1       John     Smith     25
 2         2       Joho     Smiti     26&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Jan 2019 18:52:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531112#M32940</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-01-29T18:52:11Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a JSON file in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531122#M32941</link>
      <description>&lt;P&gt;Fascinating! Thank you for the reference, other Tom!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I was only imagining a file format, as there wasn't enough information in the original post to replicate the poster's situation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was thinking the same thing...it would be pretty easy to turn the JSON Lines into JSON!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for filling in the gaps in my knowledge,&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;Tom&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 18:56:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531122#M32941</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2019-01-29T18:56:40Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a JSON file in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531191#M32942</link>
      <description>&lt;P&gt;Since you are new to the JSON format, you might (if you have not already found it) want to read &lt;A href="https://support.sas.com/resources/papers/proceedings17/SAS0380-2017.pdf" target="_self"&gt;Michael's and Eric's paper on the JSON LIBNAME engine from SAS Global Forum 2017.&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 21:41:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531191#M32942</guid>
      <dc:creator>BillM_SAS</dc:creator>
      <dc:date>2019-01-29T21:41:26Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a JSON file in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531931#M32955</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks very much for this. Your code successfully loaded it into json. However, I'm running into some issues which perhaps I could ask you about.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I run the libname json command&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname json3 JSON map=map2 automap=reuse ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The json file is parsed into a number of separate datasets.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm aware that SAS creates an ordinal variable which should allow you to link the tables back together again. However, given the structure of my json file I'm running into some issues.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="WordSection1"&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Arial',sans-serif;"&gt;Take "address" for example. The json engine is separating this into one dataset. However, a number of records contain no information of this kind.&amp;nbsp; So if the first reference of address doesn't appear until record 26 SAS is assigning an ordinal root key of 1 as it's the first time it encounters it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Arial',sans-serif;"&gt;Therefore, if I start linking back up, I'll be linking to the wrong record. This type of issues will be the same for other cases where a variable isn't on a record.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Arial',sans-serif;"&gt;So I need ensure an accurate unique ID is on each table.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Arial',sans-serif;"&gt;I have two possible workarounds, but need some guidance around them.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Arial',sans-serif;"&gt;Your code supplied below allowed me to alter the json file so I could read it into SAS.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Arial',sans-serif;"&gt;However, is it possible to extend this to create a enumeration variable called record_id which would be =1 for the first record and =n for the nth record?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  infile json2 end=eof;
  file json3 ;
  input ;
  if _n_=1 then put '[' @; else put ',' @;
  put _infile_;
  if eof then put ']';
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Arial',sans-serif;"&gt;Secondly, when I'm using the libname json command is it possible to tell SAS to ensure that the variable record_id appears on every table which is parsed?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Arial',sans-serif;"&gt;So say for table which relates to addresses, when this is created the ordinal root key would be 1,2,3 as it only encounters this variable 3 types but the record_id would be 26,500,23,000 as these are the records it relates to?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Arial',sans-serif;"&gt;I would appreciate any help on this.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 01 Feb 2019 10:54:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531931#M32955</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2019-02-01T10:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a JSON file in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531954#M32956</link>
      <description>&lt;P&gt;The MAP file is the instructions to the JSON engine about what values from the JSON object to put into which variables.&lt;/P&gt;
&lt;P&gt;The simple code you used is just letting SAS make a guess at to how you want to map that file and automatically generate a map file.&lt;/P&gt;
&lt;P&gt;You can create you own map file.&amp;nbsp; Take a look at the help pages and the map that SAS automatically generated.&lt;/P&gt;
&lt;P&gt;SAS even has a GUI tool to allow you to edit the map file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 12:57:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531954#M32956</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-02-01T12:57:11Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a JSON file in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531968#M32957</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How would I see the map which SAS generates?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 13:37:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/531968#M32957</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2019-02-01T13:37:42Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a JSON file in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/532079#M32958</link>
      <description>&lt;P&gt;An example from the on-line documentation:&lt;/P&gt;
&lt;PRE class="xis-code"&gt;libname in json 'example.json' map='user.map' automap=create;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Feb 2019 16:24:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Parsing-a-JSON-file-in-SAS/m-p/532079#M32958</guid>
      <dc:creator>BillM_SAS</dc:creator>
      <dc:date>2019-02-01T16:24:28Z</dc:date>
    </item>
  </channel>
</rss>

