<?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: Text to JSON conversion in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663172#M197916</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
length sample $32767. Phrase Key Value $32767.;
sample ="The following are the graphical (non-control) characters defined by
ISO 8859-1 (1987).  DESCRIPTION :  in words aren't all that helpful,
but they're the best we can do in text.  A graphics file illustrating
the character set should be available from the same archive as this
file.RESULT :success. INTERPRETATION : ISO 8859-1 (1987).CREATED_BY:Questy.CREATED_ON:29/07/1963";
/*
Assumptions: 
One did not know what keys would be except the fact that they precede a colon.
periods are the separators between differnt sections.
*/
put '{';
count_periods=countc(sample,'.');
do count=1 to count_periods+1;
	Phrase = scan(sample,count,'.');
	if indexc(Phrase,':') &amp;gt; 0 then do;
		Key=strip(scan(Phrase,1,':'));
		Value = strip(scan(Phrase,2,':'));
		put '"'key+(-1)'":"'Value+(-1)'"';
	end;
end;
put '}';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I modified the input by putting a period after success&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 18 Jun 2020 13:58:27 GMT</pubDate>
    <dc:creator>smantha</dc:creator>
    <dc:date>2020-06-18T13:58:27Z</dc:date>
    <item>
      <title>Text to JSON conversion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663144#M197910</link>
      <description>&lt;P&gt;I have a sample string&amp;nbsp;I need to convert the text to JSON format. I tried n number of ways but not able to achieve it, It would really sound great if anyone helps me to construct it.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;sample ="The following are the graphical (non-control) characters defined by
ISO 8859-1 (1987).  DESCRIPTION :  in words aren't all that helpful,
but they're the best we can do in text.  A graphics file illustrating
the character set should be available from the same archive as this
file.RESULT :success INTERPRETATION : ISO 8859-1 (1987).CREATED_BY:Questy.CREATED_ON:29/07/1963"

below is the required JSON output 

{
   "DESCRIPTION":" in words aren't all that helpful but they're the best we can do in text.   A graphics file illustrating the character set should be available from the same archive as thisfile",
   "RESULT":"success",
   "INTERPRETATION":" ISO 8859-1 (1987)",
   "CREATED_BY":"Questy",
   "CREATED_ON":"29/07/1963"
}&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Jun 2020 12:45:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663144#M197910</guid>
      <dc:creator>Shuail_Ibrahim</dc:creator>
      <dc:date>2020-06-18T12:45:43Z</dc:date>
    </item>
    <item>
      <title>Re: Text to JSON conversion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663160#M197913</link>
      <description>&lt;PRE&gt;data have;
sample ="The following are the graphical (non-control) characters defined by
ISO 8859-1 (1987).  DESCRIPTION :  in words aren't all that helpful,
but they're the best we can do in text.  A graphics file illustrating
the character set should be available from the same archive as this
file.RESULT :success INTERPRETATION : ISO 8859-1 (1987).CREATED_BY:Questy.CREATED_ON:29/07/1963";
run;

filename x 'c:\temp\x.json';
data _null_;
set have;
a1=find(sample,'DESCRIPTION');
a2=find(sample,'RESULT');
a3=find(sample,'INTERPRETATION');
a4=find(sample,'CREATED_BY');
a5=find(sample,'CREATED_ON');

s1=quote(substr(sample,a1+14,a2-a1-14));
s2=quote(substr(sample,a2+8,a3-a2-8));
s3=quote(substr(sample,a3+16,a4-a3-16));
s4=quote(substr(sample,a4+11,a5-a4-11));
s5=quote(substr(sample,a5+11));

file x lrecl=32767;
put '{';
put '"DESCRIPTION":'  s1 $ +(-1)  ',';
put '"RESULT":'  s2 $ +(-1) ',';
put '"INTERPRETATION":'  s3 $ +(-1)  ',';
put '"CREATED_BY":'  s4 $ +(-1) ',';
put '"CREATED_ON":'  s5 $ ;
put '}';

run;
&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Jun 2020 13:35:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663160#M197913</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-06-18T13:35:26Z</dc:date>
    </item>
    <item>
      <title>Re: Text to JSON conversion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663172#M197916</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
length sample $32767. Phrase Key Value $32767.;
sample ="The following are the graphical (non-control) characters defined by
ISO 8859-1 (1987).  DESCRIPTION :  in words aren't all that helpful,
but they're the best we can do in text.  A graphics file illustrating
the character set should be available from the same archive as this
file.RESULT :success. INTERPRETATION : ISO 8859-1 (1987).CREATED_BY:Questy.CREATED_ON:29/07/1963";
/*
Assumptions: 
One did not know what keys would be except the fact that they precede a colon.
periods are the separators between differnt sections.
*/
put '{';
count_periods=countc(sample,'.');
do count=1 to count_periods+1;
	Phrase = scan(sample,count,'.');
	if indexc(Phrase,':') &amp;gt; 0 then do;
		Key=strip(scan(Phrase,1,':'));
		Value = strip(scan(Phrase,2,':'));
		put '"'key+(-1)'":"'Value+(-1)'"';
	end;
end;
put '}';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I modified the input by putting a period after success&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jun 2020 13:58:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663172#M197916</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-06-18T13:58:27Z</dc:date>
    </item>
    <item>
      <title>Re: Text to JSON conversion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663302#M197970</link>
      <description>&lt;P&gt;This solution produces valid JSON but not exactly in the format you specified. I am not sure how strict your requirements are for the output. The advantage is that the code is simpler since once the data is in a SAS data set, PROC JSON produces the valid JSON output. I borrowed and slightly modified the DATA step that &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt; wrote to put the data into a SAS data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
sample ="The following are the graphical (non-control) characters defined by
ISO 8859-1 (1987).  DESCRIPTION :  in words aren't all that helpful,
but they're the best we can do in text.  A graphics file illustrating
the character set should be available from the same archive as this
file.RESULT :success INTERPRETATION : ISO 8859-1 (1987).CREATED_BY:Questy.CREATED_ON:29/07/1963";
run;

data work.test(drop=sample a1-a5);
set have;
a1=find(sample,'DESCRIPTION');
a2=find(sample,'RESULT');
a3=find(sample,'INTERPRETATION');
a4=find(sample,'CREATED_BY');
a5=find(sample,'CREATED_ON');

DESCRIPTION=substr(sample,a1+14,a2-a1-14);
RESULT=substr(sample,a2+8,a3-a2-8);
INTERPRETATION=substr(sample,a3+16,a4-a3-16);
CREATED_BY=substr(sample,a4+11,a5-a4-11);
CREATED_ON=substr(sample,a5+11);
run;

proc json out='c:\temp\x.json' nosastags pretty;
export work.test;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is the resultant JSON file:&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;[&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; {&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "DESCRIPTION": " in words aren't all that helpful,but they're the best we can do in text. A graphics file illustratingthe character set should be available from the same archive as thisfile.",&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "RESULT": "success",&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "INTERPRETATION": " ISO 8859-1 (1987).",&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "CREATED_BY": "Questy.",&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "CREATED_ON": "29/07/1963"&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; }&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;]&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jun 2020 20:01:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663302#M197970</guid>
      <dc:creator>BillM_SAS</dc:creator>
      <dc:date>2020-06-18T20:01:05Z</dc:date>
    </item>
    <item>
      <title>Re: Text to JSON conversion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663666#M198163</link>
      <description>thank u all for the replies, the sample string format will not be the same every time, it will change every time based on the key present in JSON we need to pick the value. sorry for mentioning this bit late</description>
      <pubDate>Sat, 20 Jun 2020 02:36:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663666#M198163</guid>
      <dc:creator>Shuail_Ibrahim</dc:creator>
      <dc:date>2020-06-20T02:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: Text to JSON conversion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663667#M198164</link>
      <description>thanks a lot, Ksharp, the sample string format will not be the same every time, it will change every time based on the key present in JSON we need to pick the value. sorry for mentioning this bit late</description>
      <pubDate>Sat, 20 Jun 2020 02:36:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663667#M198164</guid>
      <dc:creator>Shuail_Ibrahim</dc:creator>
      <dc:date>2020-06-20T02:36:56Z</dc:date>
    </item>
    <item>
      <title>Re: Text to JSON conversion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663668#M198165</link>
      <description>thanks a lot, BillM_SAS, the sample string format will not be the same every time, it will change every time based on the key present in JSON we need to pick the value. I cant hard code the position sorry for mentioning this bit late</description>
      <pubDate>Sat, 20 Jun 2020 02:38:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663668#M198165</guid>
      <dc:creator>Shuail_Ibrahim</dc:creator>
      <dc:date>2020-06-20T02:38:24Z</dc:date>
    </item>
    <item>
      <title>Re: Text to JSON conversion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663674#M198169</link>
      <description>&lt;P&gt;Below stitched together from the discussion and contributions here.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input _str $256.;
  length sampleString $ 1000;
  retain sampleString;
  sampleString=catx(' ', sampleString, _str);
  keep sampleString;
  if _n_=6 then output;
datalines;
The following are the graphical (non-control) characters defined by
ISO 8859-1 (1987).  DESCRIPTION :  in words aren't all that helpful,
but they're the best we can do in text.  A graphics file illustrating
the character set should be available from the same archive as this
file.RESULT :success INTERPRETATION : ISO 8859-1 (1987).CREATED_BY:
Questy.CREATED_ON:29/07/1963
;

options ps=max ls=max;
data parsed(keep=key value);
  set have;

  length found $2000 key $100 value $2000;
  start=1;
  stop=lengthn(sampleString);

  prxid=prxparse('/\b\w+\b\s*:.*?(?=((\b\w+\b\s*:)|$))/');
  call prxnext(prxid, start, stop, trim(sampleString), pos, len);
  do while(pos&amp;gt;0);
    found=substr(sampleString,pos,len);
    key=scan(found,1,':');
    value=scan(found,2,':');
    output;
    call prxnext(prxid, start, stop, trim(sampleString), pos, len);
  end;
run;

proc transpose data=parsed out=transposed(drop=_name_);
  id key;
  var value;
run;

filename myjson temp;
proc json out=myjson nosastags pretty;
  export transposed;
run;

data _null_;
  file print;
  infile myjson;
  input;
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1592627467729.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46407i7F58E4EE09A5FFBB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1592627467729.png" alt="Patrick_0-1592627467729.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 Jun 2020 04:31:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663674#M198169</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-06-20T04:31:19Z</dc:date>
    </item>
    <item>
      <title>Re: Text to JSON conversion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663692#M198179</link>
      <description>&lt;P&gt;OK. It is a little bit complicated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
sample ="The following are the graphical (non-control) characters defined by
ISO 8859-1 (1987).  DESCRIPTION :  in words aren't all that helpful,
but they're the best we can do in text.  A graphics file illustrating
the character set should be available from the same archive as this
file.RESULT :success INTERPRETATION : ISO 8859-1 (1987).CREATED_BY:Questy.CREATED_ON:29/07/1963";
run;
data temp;
 set have;
 n+1;
 do i=1 to countw(sample,':');
  temp=scan(sample,i,':');
  output;
 end;
drop i sample;
run;
data temp;
 set temp;
 by n;
 if not last.n then do;
   call scan(temp,-1,p,l,'_','ka');
   name=cats('"',substr(temp,p),'"');
   value=cats('"',substr(temp,1,p-1),'",') ;
 end;
 else do;name=' ';value=cats('"',temp,'"');end;
 drop p l;
run; 
data want;
 merge temp temp(keep=n value rename=(n=_n value=_value) firstobs=2);
 if n=_n;
 keep n name _value;
run;

filename x 'c:\temp\x.json';
data _null_;
set want;
by n;
file x lrecl=32767;
if first.n then put '{';
put name $ +(-1) ':' _value $;
if last.n then put '}';
run;&lt;/PRE&gt;</description>
      <pubDate>Sat, 20 Jun 2020 12:05:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Text-to-JSON-conversion/m-p/663692#M198179</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-06-20T12:05:01Z</dc:date>
    </item>
  </channel>
</rss>

