Q: I try to parse my JSON with the handy dandy “ConvertFrom-JSON” cmdlet but it only works in PowerShell 7, not in my good old PowerShell 5.1. How do I get it to work everywhere?
A: PS 7 parses JSON a little differently than PS 5.1 by ignoring comments and accepting less than ideal JSON so you need to clean up your JSON for PS 5.1. Use the function below to do it for you.
Java Script Object Notation (JSON) is a popular format these days for sending and receiving data with Azure. JSON is used for sending and receiving data using Azure REST API, deploying resources to Azure using ARM templates, configure governance in Azure using Azure Policy, and much more. PowerShell is a great tool for creating and modifying JSON. PowerShell 7.1 is my preferred solution since it has additional capabilities however sometimes we still need good old PS 5.1 so it is nice to be able to accomplish anything in either version.
Disclaimer: The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
First, here is a sample file I’ll use for the example or you can download it from here.
Alice got promoted so we need to change her department to Senior Leadership. PowerShell makes it easy to modify JSON by converting JSON to a PSCustomObject. The object can then be modified easily like any other object. The object can then be exported back out using ConvertTo-Json.