Build dynamic JSON with PowerShell

I recently did a Contacts Sync with GRAPH Api and PowerShell to create Contacts in a Shared Mailbox and USer Contact Folder.
At this time, it was needed to build dynamic JSON related to existing Values, so I looked for some posts on how to build dynamic JSON with PowerShell.

I haven’t found anything, so I decided to share my finding with you on how to save code and build dynamic JSON with PowerShell.

Simple JSON

The following Code is building a simple JSON but already shows how things work so that you can add/remove elements in the JSON.

The Code

#Simple JSON
[hashtable]$body = @{}
$Body.givenName = "Michael"
$Body.surname = "Seidl"
$Body.fileAs = "Michael Seidl"


$Json = $body | ConvertTo-Json
$Json

The Result

{
    "fileAs":  "Michael Seidl",
    "givenName":  "Michael",
    "surname":  "Seidl"
}

String Array JSON

This JSON includes a String Array Property

The Code

#String Array JSON
[hashtable]$body = @{}
$Body.givenName = "Michael"
$Body.surname = "Seidl"
$Body.fileAs = "Michael Seidl"
$Body.businessPhones = @(
                "0664 1234567"
            )

$Json = $body | ConvertTo-Json
$Json

The Result

{
    "fileAs":  "Michael Seidl",
    "givenName":  "Michael",
    "businessPhones":  [
                           "0664 1234567"
                       ],
    "surname":  "Seidl"
}

Complex Array JSON

This JSON includes a multi Property Array Property

The Code

#Complex Array JSON
[hashtable]$body = @{}
$Body.givenName = "Michael"
$Body.surname = "Seidl"
$Body.fileAs = "Michael Seidl"
$Body.emailAddresses = @([ordered]@{
    address = "ms@au2mator.com"; name = "Michael Seidl"
})

$Json = $body | ConvertTo-Json
$Json

The Result

{
    "fileAs":  "Michael Seidl",
    "givenName":  "Michael",
    "emailAddresses":  [
                           {
                               "address":  "ms@au2mator.com",
                               "name":  "Michael Seidl"
                           }
                       ],
    "surname":  "Seidl"
}

Batch Request JSON

This is an example of how to build a Batch Request JSON for the GRPAH Api Batch Requests user.

The Code

#Batch Request JSON
$i=0
[hashtable]$Array = @{}

foreach ($User in $Users) #This is an example Array with no data
{
    $i++

    [hashtable]$body = @{}
    $Body.givenName = "$($User.gn)"
    $Body.surname = "$($User.sn)"
    $Body.fileAs = "$($User.gn) $($User.sn)"
    
    $Array.requests += @([ordered]@{ 
        id      = "$i";
        method  = "POST"
        url     = "/url/"
        body    = $Body
        headers = @{"Content-Type" = "application/json; charset=utf-8" }
    })
    
}


$BatchJson = $Array | ConvertTo-Json -Depth 10
$BatchJson 

The Result

{
    "requests":  [
                     {
                         "id":  "1",
                         "method":  "POST",
                         "url":  "/url/",
                         "body":  {
                                      "fileAs":  " ",
                                      "givenName":  "",
                                      "surname":  ""
                                  },
                         "headers":  {
                                         "Content-Type":  "application/json; charset=utf-8"
                                     }
                     }
                 ]
}

This can be used as a Template for you. There is not much documentation needed though
the PowerShell Script is located on my GitHub: Seidlm/PowerShell-Tools (github.com)

Michael Seidl, aka Techguy
au2mate everything

Leave a Comment

Your email address will not be published. Required fields are marked *

*