ConvertFrom-Json and ConvertTo-Json with examples

Today in this article, we shall see useful utility methods like PowerShell ConvertFrom-Json and PowerShell ConvertTo-Json.

Today in this article, we will cover below aspects,

PowerShell ConvertTo-Json example

  • ConvertTo-Json cmdlet converts any .NET object to a string JSON.
  • This cmdlet was introduced in PowerShell 3.0.

Command Pattern

ConvertTo-Json
              -InputObject <Object>
              -Depth <Int32>
              -Compress 
              -EnumsAsStrings
              -AsArray
              -EscapeHandling <StringEscapeHandling>  [<CommonParameters>]

InputObject – This parameter specifies the objects to be converted to JSON format.

Depth– This parameter, specifies how many levels of contained objects are included in the JSON representation.

Compress – Omits white space and indented formatting in the output string.

AsArray- Let you output the object in array brackets for the input object.

EscapeHandling – Let you control characters that are escaped in the resulting JSON output

This conversion often needed when you are trying to perform GET or POST requests with body parameters understand examples.

Let’s use below sample PowerShell object which we want to represent/Generate as a JSON object using,

Example

$object = @{
    'siteUrl' ="https://www.thecodebuzz.com"
    'itemList'= @(
                    "item1",
                    "item2",
                    "item3",
                    "item4"
                )

   'details'=@(
                 @{
                  "name"= "test"
                  "id"= "213124"
                 },
                 @{
                  "name"= "test2"
                  "id"= "64564"
                 }
              )
}

ConvertTo-Json -InputObject $object

Once executed above powershell script, object shall be converted to JSON.

PowerShell ConvertFrom-Json example

  • ConvertFrom-Json cmdlet converts JSON string to object.
  • This cmdlet was introduced in PowerShell 3.0.

Command Pattern

ConvertFrom-Json

                 [-InputObject] 
                 [-AsHashtable]
                 [-Depth ]
                 [-NoEnumerate]
                 [<CommonParameters>]

InputObject – This parameter specifies the JSON string to convert to JSON format.

Depth– This parameter gets or sets the maximum depth the JSON input is allowed.

AsHashtable – Converts the JSON to a hash table object.

NoEnumerate – This parameter specifies that output is not enumerated.

Example

We shall use below Sample JSON to convert to Object

$json = @”

{
    "siteUrl": "https://www.thecodebuzz.com",
    "email": "[email protected]",
    "itemList": [
        "Item1",
        "Item2",
        "Item3"
    ],
    "details": [
        {
            "name": "test",
            "id": "213124"
        },
        {
            "name": "test2",
            "id": "453534"
        }
    ]

 }

“@

ConvertFrom-Json -InputObject $json

Using above utility methods one can send JSON data to POST request easily. Please refer below article for more detail,

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

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