Packed ZPretty.xml installs routine ZPretty in any namespace.
calling $$Do^ZPretty(input,[filler],[newline]) returns a wrapped JSON string.
filler is the optional string for the indent, default = “ “
newline is optional, default = $C(13,10)
input accepts: JSON_String, JSON_Stream, %DynamicAbstractObject
^Pretty leaves all error trapping to you. So can use it as ISJSON check.
Set flat=$$Do^ZPretty(input,””,””) removes all formatting from input.
this could be useful to compare 2 different formatted JSON_Strings.
USER>read jsn
{"Name":"Li,Robert K.","SSN":"672-92-9664","DOB":"1975-01-12","Home":{"Street":"986 Washington Blvd","City":"Boston","State":"PA","Zip":"95802"},"Office":{"Street":"6012 First Place","City":"Reston","State":"MT","Zip":"77739"},"Spouse":{"Name":"Avery,Zelda H.","SSN":"323-13-7437","DOB":"1943-03-27","Home":{"Street":"196 Main Drive","City":"Youngstown","State":"WY","Zip":"53229"},"Office":{"Street":"4056 Franklin Court","City":"Bensonhurst","State":"IA","Zip":"27688"},"FavoriteColors":["Black"],"Age":77},"Age":45,"Title":"Associate Marketing Manager","Salary":10421}
USER>
USER>write $$Do^ZPretty(jsn)
{
"Name":"Li,Robert K.",
"SSN":"672-92-9664",
"DOB":"1975-01-12",
"Home":{
"Street":"986 Washington Blvd",
"City":"Boston",
"State":"PA",
"Zip":"95802"
},
"Office":{
"Street":"6012 First Place",
"City":"Reston",
"State":"MT",
"Zip":"77739"
},
"Spouse":{
"Name":"Avery,Zelda H.",
"SSN":"323-13-7437",
"DOB":"1943-03-27",
"Home":{
"Street":"196 Main Drive",
"City":"Youngstown",
"State":"WY",
"Zip":"53229"
},
"Office":{
"Street":"4056 Franklin Court",
"City":"Bensonhurst",
"State":"IA",
"Zip":"27688"
},
"FavoriteColors":[
"Black"
],
"Age":77
},
"Age":45,
"Title":"Associate Marketing Manager",
"Salary":10421
}
USER>
USER>set wrap=$$Do^ZPretty(jsn)
USER>write $s(wrap=jsn:"SAME",1:"DIFF")
DIFF
USER>write $s($$Do^ZPretty(wrap,"","")=jsn:"SAME",1:"DIFF")
SAME
USER>set crap=$replace(wrap,"""Home"":","") USER>do Do^ZPretty(crap) <THROW>%FromJSON+38^%Library.DynamicAbstractObject.1 *%Exception.General Parsing error 3 Line 5 Offset 3
USER>try {do Do^ZPretty(jsn) Write "OK" IF 1} catch er { Write "NO" IF 0 } write !,$test
OK
1
USER>try {do Do^ZPretty(crap) Write "OK" IF 1} catch er { Write "NO" IF 0 } write !,$test
NO
0