Powershell Parse Json

Powershell and JSON Files: Everything That You Need to Know About It

These days, IT professionals are using a plethora of file formats to configure and store data. However, among them, the JSON files have become much more prominent. They are easy to use, code, and implement. Moreover, their overall security level is pretty decent as well.

But, that’s not all. These files tend to integrate perfectly with the Powershell platform too.

Therefore, if you know how to use Powershell, then you can easily deserialize JSON online. Further, you can also perform some other functional works, such as importing the files as pipeline objects, as well.

So, let’s know a little bit about the relationship between JSON files and Powershell to understand improve our overall know-how.

The Support of JSON in Powershell

How does the primary platform of Powershell integrate with JSON? Does it support file importing and exporting as well? Here, in this section, you are going to know all about the Powershell parse JSON and its basic functionalities. Let’s get started.

1.      Importing JSON Files into Powershell

To import a JSON file into Powershell, you would have to consume a JSON file first. Once you are done, you can, then, update its overall values for later usage. This particular technique is usually used while you are storing some information about a specific system.

For instance, with it, you can keep the name of the computer, its environmental properties, and many more through it. Here is an example of importing a simple JSON file, which has been named as -“computer.json”.

 {

  “SystemName”: “TestSystem”,

  “LastBoot”: “3/28/2020 11:27:15 AM”,

  “User”: “TestUser”,

  “LoggedOn”: true

}

Once you retrieve the overall content of the JSON file, you will, then, need to convert it. In this case, the output of the same would look like –

Get-Content -Path “D:\computer.json” | ConvertFrom-JSON

So, how is Powershell going to see the file of Powershell JSON parse? Well, when you are importing it, the platform will convert all of its information into “PSCustomObject” and attempt to make the properties equivalent alongside their types, like “bool” or “string”.

2.      Exporting the JSON Files from PowerShell

Just like importing, exporting JSON files can also be done in Powershell. However, before doing so, you will need to make some changes or modifications to the file. Once you are done, you can add a new property on the same through the below-mentioned coding.

$JSON = Get-Content -Path “D:\computer.json” | ConvertFrom-JSON

$JSON | Add-Member -Name “PSVersion” -MemberType NoteProperty -Value “7.0”

So, as you have already added the brand new property in the object, now you can move on and export it for your purpose. Here’s the code that can help you out.

1 $JSON | ConvertTo-JSON | Out-File “D:\computer.json”

 Features of Powershell

There are two different features available in the new update of Powershell. They can be used for maintaining, updating, importing, and exporting the JSON files made with YAML parser python. These are –

  1. ConvertTo-JSON

This feature comes with two different parameters, which are –

  • EnumsAsStrings
  • AsArray
  1. ConvertFrom-JSON

Like the previous one, the ConvertFrom-JSON, too, comes with a parameter. It is –

  • NoEnumerate

Manipulating and using JSON content in the Powershell platform is pretty easy. However, you would still need to learn a little bit about it to improve your overall proficiency even more.