<?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: transform dataset to a specific json format with condition in Developers</title>
    <link>https://communities.sas.com/t5/Developers/transform-dataset-to-a-specific-json-format-with-condition/m-p/887845#M6391</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data testdataset;
  input sku$ label$ search__XX$ search__YY$ enabled;
datalines;
123-456 productA  producAX  productAY 1
123-789 productB  producBX  productBY 0
456-789 productC  producCX  productCY 1
;
data _null_;
 set testdataset;
 file "c:\temp\want.json";
_enabled=ifc(enabled=1,'true ','false');
put '{"identifier":"' sku +(-1)'","values":{"label":[{"scope":null,"data":"' 
    label +(-1)'"}],"search":[{"scope":"XX","data":"' 
    search__XX  +(-1)'"},{"scope":"YY","data":"'  
    search__YY  +(-1)'"}],"enabled":[{"scope":null,"data":'
    _enabled +(-1)'}]}}' ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 04 Aug 2023 17:47:58 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2023-08-04T17:47:58Z</dc:date>
    <item>
      <title>transform dataset to a specific json format with condition</title>
      <link>https://communities.sas.com/t5/Developers/transform-dataset-to-a-specific-json-format-with-condition/m-p/887723#M6390</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I'm pretty new to SAS and not sure if we can achieve it, but i try to generate a json form a table.&lt;BR /&gt;&lt;BR /&gt;Let say I get this dataset :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data testdataset;
  input sku$ label$ search__XX$ search__YY$ enabled;
datalines;
123-456 productA  producAX  productAY 1
123-789 productB  producBX  productBY 0
456-789 productC  producCX  productCY 1
;&lt;/PRE&gt;&lt;P class=""&gt;&amp;nbsp;&lt;/P&gt;&lt;P class=""&gt;&lt;BR /&gt;I want to transform it to&lt;/P&gt;&lt;P class=""&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;{"identifier":"123-456","values":{"label":[{"scope":null,"data":"productA"}],"search":[{"scope":"XX","data":"producAX"},{"scope":"YY","data":"productAY"}],"enabled":[{"scope":null,"data":true}]}}
{"identifier":"123-789","values":{"label":[{"scope":null,"data":"productB"}],"search":[{"scope":"XX","data":"producBX"},{"scope":"YY","data":"productBY"}],"enabled":[{"scope":null,"data":false}]}}
{"identifier":"456-789","values":{"label":[{"scope":null,"data":"productC"}],"search":[{"scope":"XX","data":"producCX"},{"scope":"YY","data":"productCY"}],"enabled":[{"scope":null,"data":true}]}}&lt;/PRE&gt;&lt;P class=""&gt;I try do understand the doc of proc json but when i try something like&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;proc json out="&amp;amp;ftpit\jsontest.json" pretty nosastags; 
     export testdataset; 
run;&lt;/PRE&gt;&lt;P class=""&gt;&lt;BR /&gt;I got extra array [ ] on my json, and also a comma between each items and i'm struggle to "split" my columns inside the "value" tag, but also be able to put specific column inside the right value&lt;BR /&gt;&lt;BR /&gt;Literraly i think i need to do something like a loop for each row of my dataset and&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;-create json&lt;BR /&gt;- create key idendifier and put testdataset.sku&lt;/P&gt;&lt;P class=""&gt;-create key values&lt;BR /&gt;- create key label and put&amp;nbsp;[{"scope":null,"data":testdataset.label}]&lt;BR /&gt;- put coma&lt;BR /&gt;- create key search and put&amp;nbsp;[{"scope":"XX","data":testdataset.search__XX},{"scope":"YY","data":testdataset.search__YY}]&lt;BR /&gt;- put coma&lt;BR /&gt;- create key&amp;nbsp;"enabled" and put [{"scope":null,"data": case when testdataset.enabled = 1 then true else false }]&lt;BR /&gt;- close json&lt;BR /&gt;&lt;BR /&gt;not sure if it's something doable via sas ?&lt;BR /&gt;&lt;BR /&gt;Thanks for your help&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2023 14:57:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/transform-dataset-to-a-specific-json-format-with-condition/m-p/887723#M6390</guid>
      <dc:creator>mitchum</dc:creator>
      <dc:date>2023-08-03T14:57:05Z</dc:date>
    </item>
    <item>
      <title>Re: transform dataset to a specific json format with condition</title>
      <link>https://communities.sas.com/t5/Developers/transform-dataset-to-a-specific-json-format-with-condition/m-p/887845#M6391</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data testdataset;
  input sku$ label$ search__XX$ search__YY$ enabled;
datalines;
123-456 productA  producAX  productAY 1
123-789 productB  producBX  productBY 0
456-789 productC  producCX  productCY 1
;
data _null_;
 set testdataset;
 file "c:\temp\want.json";
_enabled=ifc(enabled=1,'true ','false');
put '{"identifier":"' sku +(-1)'","values":{"label":[{"scope":null,"data":"' 
    label +(-1)'"}],"search":[{"scope":"XX","data":"' 
    search__XX  +(-1)'"},{"scope":"YY","data":"'  
    search__YY  +(-1)'"}],"enabled":[{"scope":null,"data":'
    _enabled +(-1)'}]}}' ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Aug 2023 17:47:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/transform-dataset-to-a-specific-json-format-with-condition/m-p/887845#M6391</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-08-04T17:47:58Z</dc:date>
    </item>
    <item>
      <title>Re: transform dataset to a specific json format with condition</title>
      <link>https://communities.sas.com/t5/Developers/transform-dataset-to-a-specific-json-format-with-condition/m-p/887875#M6392</link>
      <description>&lt;P&gt;That is not a JSON file.&amp;nbsp; Perhaps you meant that you wanted to create a &lt;A href="https://jsonlines.org/" target="_self"&gt;JSONL file&lt;/A&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Aug 2023 14:06:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/transform-dataset-to-a-specific-json-format-with-condition/m-p/887875#M6392</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-04T14:06:51Z</dc:date>
    </item>
    <item>
      <title>Re: transform dataset to a specific json format with condition</title>
      <link>https://communities.sas.com/t5/Developers/transform-dataset-to-a-specific-json-format-with-condition/m-p/888050#M6393</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I finally get something working by doing without the proc json but doing that :&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;data testdataset;
  input sku$ label$ search__XX$ search__YY$ enabled;
datalines;
123-456 productA  producAX  productAY 1
123-789 productB  producBX  productBY 0
456-789 productC  producCX  productCY 1
;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data _null_;
  set temp;
  filename payload temp;
  /* Create an array to store key-value pairs */
  array json_arr[100] $1000 _temporary_;

  /* Generate the JSON payload for each observation */
  length json_in_str $32767; /* Adjust the length as needed */
  retain json_in_str;
  file payload; /* Create a temporary file for the JSON payload */
  /*file print; */
  *filename payload clear;
  
  /* Start building the JSON payload for each observation */
  json_in_str = '{"identifier":"' || compress(sku) || '","enabled":' || put(enabled, 1. -l) || ',"values":{';
  
  /* Add the key-value pairs to the array */
  /*json_arr[1] = '';*/
  json_arr[3] = ' "availibility_code":[{"scope":null,"locale":null,"data":"' || availibility_code || '"}],';
  json_arr[6] = ' "ean":[{"scope":null,"locale":null,"data":"' || compress(ean) || '"}],';
  json_arr[17] = ' "label":[{"scope":"XX","locale":null,"data":"' || compress(label__XX) || '"},{"scope":"YY","locale":null,"data":"' || compress(label__YY) || '"}],';
  &lt;BR /&gt;   ....
  
  /* Combine all the key-value pairs from the array */
  do i = 1 to dim(json_arr);
    if not missing(json_arr[i]) then
      json_in_str = catx(' ', json_in_str, json_arr[i]);
  end;

  /* Finish building the JSON payload */
  json_in_str = catx(' ', json_in_str, '}}');
  put json_in_str; /* Output the JSON payload for the current observation */
run;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;So basically i end up with the expected json format.&lt;BR /&gt;&lt;BR /&gt;Thanks&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Aug 2023 07:54:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/transform-dataset-to-a-specific-json-format-with-condition/m-p/888050#M6393</guid>
      <dc:creator>mitchum</dc:creator>
      <dc:date>2023-08-06T07:54:12Z</dc:date>
    </item>
    <item>
      <title>Re: transform dataset to a specific json format with condition</title>
      <link>https://communities.sas.com/t5/Developers/transform-dataset-to-a-specific-json-format-with-condition/m-p/888064#M6394</link>
      <description>&lt;P&gt;If you are going to write a text file anyway you can let the PUT statement to most of the work.&amp;nbsp; So no need to construct the line in a character variable and then write.&amp;nbsp; Just write it like you would when writing out any other text file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename payload temp;

data _null_;
  set temp;
  file payload;  
  put '{"identifier":' sku :$quote. ',"enabled":' enabled ',"values":{' @;
  
etc.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Plus that means the lines in your JSON LINES file are not limited to 32,767 bytes of a single dataset variable.&amp;nbsp; &amp;nbsp;The LRECL= option of the FILE statement will support much longer lines.&amp;nbsp; Or you could use RECFM=N and write the end of line characters yourself using PUT '0DOA'x ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NOTE: You are still not creating an actual JSON file.&amp;nbsp; Instead each line in your file is an complete JSON file.&amp;nbsp; So that is a JSON LINES file. For the whole file to be valid JSON you would need to combine the lines into a JSON object or array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Aug 2023 13:48:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/transform-dataset-to-a-specific-json-format-with-condition/m-p/888064#M6394</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-06T13:48:16Z</dc:date>
    </item>
  </channel>
</rss>

