Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Json Formatting

I need some help with formatting json data. I'm converting a table to json to be ingested into a log collector. The collector only likes json in single line format. How can I convert this json:

 [
     {  "Severity":  "Informational",
         "Type":  "Milestone",
         "SiteCode":  "ABC",
         "DateTime":  1505840220813,
         "System":  "Server.domain.local",
         "Component":  "SMS_Distribution_Point_Monitoring",
         "Module":  "SMS Server",
         "MessageID":  2380,
         "Description":  "Start to evaluate package ABC001F5 on distribution point  Display=\\\\Server.domain.local\\ MSWNET: SMS_SITE=ABC \\\\Server.domain.local\\."
     },
     {  "Severity":  "Informational",
         "Type":  "Milestone",
         "SiteCode":  "ABC",
         "DateTime":  1505840220787,
         "System":  "Server.domain.local",
         "Component":  "SMS_Distribution_Point_Monitoring",
         "Module":  "SMS Server",
         "MessageID":  2384,
         "Description":  "Package ABC0019F on distribution point  Display=\\\\Server.domain.local\\ MSWNET: SMS_SITE=ABC \\\\Server.domain.local\\ has been verified successfully."
     }
 ]

to this in powershell when outputting to a file:

 [{"Severity":"Informational","Type":"Milestone","SiteCode":"ABC","DateTime":1505840220813,"System":"Server.thecarlylegroup.local","Component":"SMS_Distribution_Point_Monitoring","Module":"SMSServer","MessageID":2380,"Description":"StarttoevaluatepackageABC001F5ondistributionpointDisplay=\\\\Server.thecarlylegroup.local\\MSWNET:SMS_SITE=ABC\\\\Server.thecarlylegroup.local\\."},
 {"Severity":"Informational","Type":"Milestone","SiteCode":"ABC","DateTime":1505840220787,"System":"Server.thecarlylegroup.local","Component":"SMS_Distribution_Point_Monitoring","Module":"SMSServer","MessageID":2384,"Description":"PackageABC0019FondistributionpointDisplay=\\\\Server.thecarlylegroup.local\\MSWNET:SMS_SITE=ABC\\\\Server.thecarlylegroup.local\\hasbeenverifiedsuccessfully."}]
like image 527
Patrick Avatar asked Dec 03 '25 23:12

Patrick


1 Answers

Assuming you get your data out of your table and into a string similar to the following:

$json = @"
[
     {  "Severity":  "Informational",
         "Type":  "Milestone",
         "SiteCode":  "ABC",
         "DateTime":  1505840220813,
         "System":  "Server.domain.local",
         "Component":  "SMS_Distribution_Point_Monitoring",
         "Module":  "SMS Server",
         "MessageID":  2380,
         "Description":  "Start to evaluate package ABC001F5 on distribution point  Display=\\\\Server.domain.local\\ MSWNET: SMS_SITE=ABC \\\\Server.domain.local\\."
     },
     {  "Severity":  "Informational",
         "Type":  "Milestone",
         "SiteCode":  "ABC",
         "DateTime":  1505840220787,
         "System":  "Server.domain.local",
         "Component":  "SMS_Distribution_Point_Monitoring",
         "Module":  "SMS Server",
         "MessageID":  2384,
         "Description":  "Package ABC0019F on distribution point  Display=\\\\Server.domain.local\\ MSWNET: SMS_SITE=ABC \\\\Server.domain.local\\ has been verified successfully."
     }
]
"@

Then you can do this (the depth was chosen arbitrarily to ensure that the entire object was converted):

$compressedJson = $json | ConvertFrom-Json | ConvertTo-Json -Compress -Depth 100
like image 162
Jason Boyd Avatar answered Dec 05 '25 12:12

Jason Boyd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!