3

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."}]

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

I'm actually outputting to Json and then removing some invalid data as well. $StatusMessagesJson = $StatusMessages| ConvertTo-Json $StatusMessagesJson -replace [Regex]::Escape('"\/Date('),'' -replace [Regex]::Escape(')\/"'),"" | Set-Content $Path
0

If you're working a string:

$var = (@"
[
     {  "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."
     }
]
"@ -split "`n" | % { $_.Trim() }) -join ''

1 Comment

its not a string, its a table being converted to json as it states

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.