BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
HB
Barite | Level 11 HB
Barite | Level 11

Hey,

 

I need to generate JSON that looks like 

 

...

"key1": "value1",
"key2": ["arrayvalue1", "arrayvalue2", "arrayvalue3"],

...

 

The above format works and displays when I feed it to a Web page.

 

I am trying to use 

 

proc json out = "mylocation\filename.json" pretty nosastags;
export myoutputtable;
run;

 

This runs/works (and could be a cool time saver), but delivers JSON looking like 

 

...

"key1": "value1",
"key2": "[\"arrayvalue1\", \"arrayvalue2\", \"arrayvalue3\"]",

...

 

So it quotes the whole array and escapes the quotes on the items inside.  Blows things up. 

 

Can anybody tell me how or even if I can tweak Proc JSON to adjust the output?

 

If I can't, I sort of get this solution from Tom

https://communities.sas.com/t5/SAS-Procedures/Proc-JSON-Export-Nested-JSON/m-p/813357

but don't readily see how to adapt that writing a value array with variable item counts.  

 

Thanks in advance. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Instead of using code like this 

  /' ,"URL":{"path":' url :$quote. '}'

to create a JSON object you could use code like this:

/' ,"key2": [' stringvar ']'

to create a JSON array.  Where KEY2 is the name for the array and STRINGVAR is the single variable with a value like:

"arrayvalue1", "arrayvalue2", "arrayvalue3"

Or use a DO loop to write multiple individual values.  Either by looping over an array (SAS data step array) of the values, only writing the non-missing values.  Or using BY group processing to write values from multiple observations of the same variable.

 

PS Again, write the continuation characters at beginning of the lines where they are easier for humans to scan.

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Instead of using code like this 

  /' ,"URL":{"path":' url :$quote. '}'

to create a JSON object you could use code like this:

/' ,"key2": [' stringvar ']'

to create a JSON array.  Where KEY2 is the name for the array and STRINGVAR is the single variable with a value like:

"arrayvalue1", "arrayvalue2", "arrayvalue3"

Or use a DO loop to write multiple individual values.  Either by looping over an array (SAS data step array) of the values, only writing the non-missing values.  Or using BY group processing to write values from multiple observations of the same variable.

 

PS Again, write the continuation characters at beginning of the lines where they are easier for humans to scan.

HB
Barite | Level 11 HB
Barite | Level 11

Tom,

 

Your repost of the $quote. thing was what did it.  

 

I manipulated my programtype array data to look like this as a string:

["11", "10", "9", "16", "14", "1"]

 

then I wrote it out as 

"programname":' programname :$quote. /',
"programtype":' programtype /',

 

without the outside quoting.

 

That gives me the output I want

, "programname":"name"
, "programtype":["1", "16"] 

 

except that (unlike you, and I know this will probably tick you off - LOL) I HATE with a passion the commas at the beginning of the line  and would really prefer

"programname":"name",
"programtype":["1", "16"],

 

I know the arguments, I just like them at the end. I don't know how to move them so i guess I am stuck until I figure that out. 

 

Thanks for tweaking my brain. 

 

 

 

 

 

HB
Barite | Level 11 HB
Barite | Level 11

Yep.  

 

This

{

item

,item

,item

}

 

doesn't work. Never loads. 

 

I think I do need

{

item,

item,

item

}

 

for the code to like and display it. 

 

I've tried every rearrangement of

 "key":' dataset_value :$quote. /',

 

I can think of with no joy. 

 

"key":' dataset_value :$quote.  ,/'
 "key":' dataset_value :$quote. ' ,/

"key":' dataset_value :$quote. ' , /n

 

How do I do it?  Looking at you Tom. 

 

SAS Innovate 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 735 views
  • 0 likes
  • 2 in conversation