Create JSON Array in PowerShell

Create JSON Array in PowerShell

  1. Use ConvertTo-Json to Create a JSON Array in PowerShell
  2. Use ConvertTo-Json to Create a JSON Array and Save It Into a File in PowerShell

JSON is a standard file format and data interchange format. It uses human-readable text to store data and transmit data. It consists of arrays and attributes value pairs.

Through PowerShell, you can generate a JSON array. This article will show how we can create a JSON array using PowerShell.

Also, we will see necessary examples and explanations to make the topic easier. We will see two different ways to create a JSON array.

Use ConvertTo-Json to Create a JSON Array in PowerShell

In this section, we will organize a full JSON array inside a variable and then convert it to a JSON array using ConvertTo-Json.

In our example below, we created a simple JSON array using this method. The code for our example will be as follows:

$Body = @(
    @{
        FirstName='Alen'
        LastName='Walker'
    }
)
ConvertTo-Json -InputObject $Body

In the example above, we created the whole JSON array inside the variable $Body. After that, we converted it to a JSON array using the line ConvertTo-Json -InputObject $Body,

After running the above example, you will get an output like the following:

[
    {
        "LastName":  "Walker",
        "FirstName":  "Alen"
    }
]

In our next example, we created the sub-array elements. The code for this example is as follows:

$body = @{
    'My Items'= @(
                    "Item 1",
                    "Item 2",
                    "Item 3",
                    "Item 4"
                )

   'My details'=@(
                 @{
                  "Name"= "Alen"
                  "Id"= "0001"
                 },
                 @{
                  "Name"= "Mycle"
                  "Id"= "0002"
                 }
              )
}
ConvertTo-Json -InputObject $Body

After running the above example, you will get this output:

{
    "My details":  [
                       {
                           "Id":  "0001",
                           "Name":  "Alen"
                       },
                       {
                           "Id":  "0002",
                           "Name":  "Mycle"
                       }
                   ],
    "My Items":  [
                     "Item 1",
                     "Item 2",
                     "Item 3",
                     "Item 4"
                 ]
}

Use ConvertTo-Json to Create a JSON Array and Save It Into a File in PowerShell

This method is a bit complex. We will create the JSON array step by step using more than one variable.

Please take a look at the following example to understand this method better.

$jsonBase = @{}
$array = @{}
$data = @{"Name"="Mycle";"Colour"="Alen";}
$array.Add("User",$data)
$jsonBase.Add("UserData",$array)
$jsonBase | ConvertTo-Json -Depth 10 | Out-File ".\sample.json"

In the example shared above, we first created a variable for the array’s base using the line $jsonBase = @{}. Then, we created another variable for the array element using the line $array = @{}.

After that, we declared another variable to hold the array date and combined the array and its subarray. Lastly, we converted it to a JSON array and saved the array as a .json file.

After executing the above PowerShell script, you will see that a file named sample.json is created in your working directory with the contents below:

{
    "UserData":  {
                     "User":  {
                                  "Name":  "Mycle",
                                  "Colour":  "Alen"
                              }
                 }
}

We discussed two methods in this article. You can use them based on your requirements.

Please note that the example codes shared here are only executable on the Windows PowerShell environment.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - PowerShell JSON

  • Loop Through a JSON File in PowerShell
  • Read JSON Files in PowerShell
  • Write JSON Object to a File in PowerShell
  • Parse JSON Files Using PowerShell