Work with JSON data in PowerShell

Introduction

JSON is a well known, flexible format that allows to easily store the information. PowerShell provides a great cmdlets that helps working with JSON formatted data.

Let’s consider the sample JSON structure, containing information about the colors:

ConvertFrom-Json

The cmdlet allows easily to convert JSON data into the PowerShell custom object

ConvertTo-Json

The same way, the cmdlet allows to convert custom PowerShell object to JSON format.

This approach provides a great flexibility to quickly import and save the data that can be used within PowerShell scripts. It is getting more tricky when the JSON structure or PowerShell scripts logic is getting more and more complex. In addition, it might be also challenging to someone who is not aware of the data structure to start working on the .ps1 files.

The ideal situation would be to bring some kind of strong typing and intellisense to the PowerShell experience. We can use VS Code which is capable of resolving object structure, after loading it into memory.

20190101_VScode0

Now, we know the structure of the initialized object that we are working with and it solves partially the problem, but still when the complexity of the logic grows or we create custom functions that use argument names different than the loaded object name, we loose the syntax highlighting.

20190101_VScode1

PowerShell class for the rescue

To improve the development experience, we can create custom PowerShell classes, which will help us to get strongly typed objects in both the development and runtime scenarios. Let’s create simple classes that are going to represent the JSON data structure:

Now, we can use the Color class, as a parameter type for the Get-ColorDetails function. Thanks to that, we regain the syntax highlighting in VS Code.

20190101_VScode2

This approach will ensure that the Colors class will be initialized correctly. Moreover, we do not have to remove some additional information stored in the JSON like e.g. $schema.

VS Code Profile file

To fully leverage the described approach, we need to load the definition of the model into the ‘PowerShell Integrated Console’ every time we open the project. To automate the task and provide the same experience every time, let’s create a VS Code profile file, which is going to load the class definitions.

In the ‘PowerShell Integrated Console’, execute:

notepad $profile

Or create it if doesn’t exist:

New-Item $profile -type File

In the notepad, execute the model script (ColorsModel.ps1), to ensure that our class definition has been loaded:

if (Test-Path './ColorsModel.ps1') {
. ./ColorsModel.ps1
}

Save the file and restart the ‘Powershell Integrated Console’.  Now you can leverage syntax highlighting and strongly typed objects inside your PowerShell functions.

The full sample can be found in my github:
https://github.com/siata13/powershell-json-model

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s