Hello everyone,
New SAS user and beginner programmer in general, so this may be a very basic question and the terminology might be a bit off. I am currently trying to create a JSON file which is going to be posted to an API. This is all done within DI Studio. A new requirement to the JSON-file format have me somewhat confused as to what the best solution would be. Below is a sample of the desired output of the JSON file.
[
{
"Store":{
"StoreCode":"Aldi"
},
"Date": "2021-02-08",
"CustomerCount": 11000,
"CustomerSpending": 800
}
]
The issue for me is creating this "nested" key-value pair ("StoreCode":"Aldi") part which is highlighted in Bold. Currently I've tried to work around this using the following code:
substr(compress('{"StoreCode": ' || '"' || Store|| '"' || '}'), 1, 25)
This is obviously not a good solution as this will come through as a string. Is there a way for SAS to create nested key-value pairs like in the sample above?
Since you are new to SAS may I say welcome to the SAS Communities. May I suggest you look at the JSON procedure for producing the JSON file. I assume that will be available to you in DI Studio. I have not used DI Studio.
It will take some extra work to produce the hierarchical JSON file with the JSON procedure. Since you did not post the data set from which you are creating the JSON, I cannot give you a working example creating the JSON file. I suggest you take a look at the following posts to get an idea of how this can be done with the JSON procedure:
https://communities.sas.com/t5/Developers/Exporting-Nested-Tables-to-JSON/m-p/717112#M1122
https://communities.sas.com/t5/SAS-Canada-Discussion-Forum/Formattting-JSON/m-p/688562#M224
Since you are new to SAS may I say welcome to the SAS Communities. May I suggest you look at the JSON procedure for producing the JSON file. I assume that will be available to you in DI Studio. I have not used DI Studio.
It will take some extra work to produce the hierarchical JSON file with the JSON procedure. Since you did not post the data set from which you are creating the JSON, I cannot give you a working example creating the JSON file. I suggest you take a look at the following posts to get an idea of how this can be done with the JSON procedure:
https://communities.sas.com/t5/Developers/Exporting-Nested-Tables-to-JSON/m-p/717112#M1122
https://communities.sas.com/t5/SAS-Canada-Discussion-Forum/Formattting-JSON/m-p/688562#M224
Thank you @BillM_SAS, worked like a charm
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: