<?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 Help reading a CSV file with a JSON field that has multi-level data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-reading-a-CSV-file-with-a-JSON-field-that-has-multi-level/m-p/541205#M149387</link>
    <description>&lt;P&gt;A few months ago, I asked for help reading a CSV with a JSON field and you guys came up with a solution that worked perfectly... now I got an even bigger problem.&amp;nbsp; The fields in the JSON are more complex:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cat_ID, JSON&lt;/P&gt;
&lt;P&gt;1234,{"Name":"Buttons","Age":3,"Likes":[{"Item":"Food","Name":"Tuna"},{"Item":"Food","Name":"Chicken"},{"Item":"Toy","Name":"Laser Pointer"}],"Dislikes":[{"Item":"Food","Name":"Lemons"},{"Item":"Cat","Name":"Mittens"}],"Color":"Orange"}&lt;/P&gt;
&lt;P&gt;1121,{"Name":"Mittens","Age":5,"Dislikes":[{"Item":"Cat","Name":" Buttons"}],"Color":"Black"}&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first record has multiple sub fields in the Likes and Dislikes categories.&amp;nbsp; The second record has just 1 Dislike.&amp;nbsp; There could be 0 to many Likes and Dislikes.&amp;nbsp; Also every field in each category uses the same field name.&amp;nbsp; So I can't just brute force combine whatever is before a colon to whatever is after.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ideally I want to end up with 3 tables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Details that contain: Cat_ID, Name, Age and Color.&lt;/P&gt;
&lt;P&gt;Likes that contain:&amp;nbsp;Cat_ID, Item and Name&lt;/P&gt;
&lt;P&gt;Dislikes that contain:&amp;nbsp;Cat_ID, Item and Name&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to parse by commas, but using the square brackets are delimiters?&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 07 Mar 2019 20:05:40 GMT</pubDate>
    <dc:creator>Areksoo</dc:creator>
    <dc:date>2019-03-07T20:05:40Z</dc:date>
    <item>
      <title>Help reading a CSV file with a JSON field that has multi-level data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-reading-a-CSV-file-with-a-JSON-field-that-has-multi-level/m-p/541205#M149387</link>
      <description>&lt;P&gt;A few months ago, I asked for help reading a CSV with a JSON field and you guys came up with a solution that worked perfectly... now I got an even bigger problem.&amp;nbsp; The fields in the JSON are more complex:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cat_ID, JSON&lt;/P&gt;
&lt;P&gt;1234,{"Name":"Buttons","Age":3,"Likes":[{"Item":"Food","Name":"Tuna"},{"Item":"Food","Name":"Chicken"},{"Item":"Toy","Name":"Laser Pointer"}],"Dislikes":[{"Item":"Food","Name":"Lemons"},{"Item":"Cat","Name":"Mittens"}],"Color":"Orange"}&lt;/P&gt;
&lt;P&gt;1121,{"Name":"Mittens","Age":5,"Dislikes":[{"Item":"Cat","Name":" Buttons"}],"Color":"Black"}&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first record has multiple sub fields in the Likes and Dislikes categories.&amp;nbsp; The second record has just 1 Dislike.&amp;nbsp; There could be 0 to many Likes and Dislikes.&amp;nbsp; Also every field in each category uses the same field name.&amp;nbsp; So I can't just brute force combine whatever is before a colon to whatever is after.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ideally I want to end up with 3 tables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Details that contain: Cat_ID, Name, Age and Color.&lt;/P&gt;
&lt;P&gt;Likes that contain:&amp;nbsp;Cat_ID, Item and Name&lt;/P&gt;
&lt;P&gt;Dislikes that contain:&amp;nbsp;Cat_ID, Item and Name&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to parse by commas, but using the square brackets are delimiters?&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 20:05:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-reading-a-CSV-file-with-a-JSON-field-that-has-multi-level/m-p/541205#M149387</guid>
      <dc:creator>Areksoo</dc:creator>
      <dc:date>2019-03-07T20:05:40Z</dc:date>
    </item>
    <item>
      <title>Re: Help reading a CSV file with a JSON field that has multi-level data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-reading-a-CSV-file-with-a-JSON-field-that-has-multi-level/m-p/541208#M149390</link>
      <description>&lt;P&gt;Best bet would be convert the file to a valid JSON file and then read it using the JSON libname engine.&lt;/P&gt;
&lt;P&gt;Perhaps something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename have 'myfile.csv';
filename fix temp;
data _null_;
  infile have dlm=',' firstobs=2 end=eof truncover;
  file fix  ;
  input cat_id +1 line $10000. ;
  if _n_=1 then put '[' @; else put ',' @;
  put '{"Cat_id":' cat_id ',' line ;
  if eof then put ']';
run;

data _null_;
  infile fix;
  input;
  list;
run;
filename map temp;
libname json json fileref=fix map=map automap=reuse;
proc copy inlib=json outlib=work;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Mar 2019 20:23:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-reading-a-CSV-file-with-a-JSON-field-that-has-multi-level/m-p/541208#M149390</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-03-07T20:23:31Z</dc:date>
    </item>
    <item>
      <title>Re: Help reading a CSV file with a JSON field that has multi-level data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-reading-a-CSV-file-with-a-JSON-field-that-has-multi-level/m-p/541210#M149392</link>
      <description>&lt;PRE&gt;       ordinal_
Obs      root      Cat_id     Name      Age    Color

 1         1        1234     Buttons     3     Orange
 2         2        1121     Mittens     5     Black
&amp;#12;

       ordinal_    ordinal_
Obs      root        Likes     Item    Name

 1         1           1       Food    Tuna
 2         1           2       Food    Chicken
 3         1           3       Toy     Laser Pointer
&amp;#12;

       ordinal_    ordinal_
Obs      root      Dislikes    Item      Name

 1         1           1       Food    Lemons
 2         1           2       Cat     Mittens
 3         2           3       Cat      Buttons&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Mar 2019 20:27:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-reading-a-CSV-file-with-a-JSON-field-that-has-multi-level/m-p/541210#M149392</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-03-07T20:27:17Z</dc:date>
    </item>
    <item>
      <title>Re: Help reading a CSV file with a JSON field that has multi-level data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-reading-a-CSV-file-with-a-JSON-field-that-has-multi-level/m-p/541271#M149413</link>
      <description>&lt;P&gt;So I had to play around with my data file a bit as I was having issues like the JSON field getting double quoted, but once I got it to a usable state, your code worked perfectly!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 23:06:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-reading-a-CSV-file-with-a-JSON-field-that-has-multi-level/m-p/541271#M149413</guid>
      <dc:creator>Areksoo</dc:creator>
      <dc:date>2019-03-07T23:06:22Z</dc:date>
    </item>
  </channel>
</rss>

