My ConvertTo-Hashtable function allows you to convert JavaScript Object Notation (JSON) data format to a native PowerShell hash table.

Adam Bertram

Adam Bertram is a 20-year IT veteran, Microsoft MVP, blogger, and trainer. Adam is the founder of the e-learning tech screencast platform TechSnips. Catch up on Adam’s articles at adamtheautomator.com, or follow TechSnips on Twitter at @techsnips_io.

We can think of PowerShell as automation glue. This language seems to be able to interact with just about every conceivable product. Part of this impartial ability to work with lots of products typically means working with application programming interfaces (APIs). Many services these days have a representational state transfer (REST) API.

PowerShell can easily query these REST APIs using the Invoke-RestMethod command or Invoke-Webrequest. But what then? Lots of APIs return JSON, which is great. But what if I'm going to use this data in other parts of my script? It's possible to carry around a JSON string all day in a script, but it's going to be tough to parse that out and get to the data you need. Instead, it's much better to convert that JSON string to a native hash table.

PowerShell has a native command called ConvertFrom-Json, but it doesn't convert data to a hash table. In simple situations, this may be fine. But I've found when you've got JSON data that contains deeply nested arrays, for example, you'll begin to see some problems. This command does some processing under the hood as well, like performing some escaping I sometimes don't want. So I decided to build a ConvertTo-Hashtable function that takes an object returned from ConvertFrom-Json and converts it to a hash table.

To demonstrate, let's take a JSON string as an example.

I'll then assign that JSON data to a variable to create a string and then convert it to a PowerShell PSCustomObject using the ConvertFrom-Json command:

ConvertFrom Json

ConvertFrom Json

I now have an object I can work with. I'll go ahead and assign the output to the $jsonObj variable. At this point, I want to convert this object to a hash table. If the object just had some simple string properties, this would not be a hard task at all. We'd simply enumerate all the properties of the object and create a new hash table from them.

We'd be done here if it weren't for the possibility that these properties can be a lot of different types other than just strings. To build a robust conversation function, we need to account for these situations. This is where I'd usually break out each piece of the code and explain it independently. Since this code is so tightly integrated as well as being a recursive function, it will be easier to explain what's going on inside the function comments themselves.

Performing conversions like this can be challenging and will sometimes require lots of trial and error. I created this function in just this way. An object can be in so many variations. Thus, the only true way to create code that doesn't break will be simply to run as much input through it as possible and monitor the results.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

2+
Share
7 Comments
  1. Leonard 2 years ago

    Thanks for the explanation and great code. It works perfectly on a json configuration block with nested hashtables and arrays.

    A minor correction on the end of the script. The last line should be $jsonString, not $jsonObj
    $jsonString | ConvertFrom-Json | ConvertTo-HashTable

    0

    • Michael Pietroforte 2 years ago

      Where did you get the $jsonString variable from?

      0

      • Leo 2 years ago

        $jsonObj is the json-decoded object, as seen on the article's line "I now have an object I can work with. I'll go ahead and assign the output to the $jsonObj variable."

        $jsonString is, basically, the $json variable

        0

        • Michael Pietroforte 2 years ago

          You are right, the article was somewhat incomplete and contained errors. I fixed it now. The crucial line for you is the last one:

          0

  2. MatthGyver 2 years ago

    Hi,

    I'm working on Powershell module to manage Grafana application and plan to put it on Github in opensource.

    Can I use your code as is by mentioning your name and this url page please ?

    Thank you

    0

  3. rock 11 months ago

    When we conver it back to Json, why it  have additional  "value" , and "count"?
    $jsonObj = $json | ConvertFrom-Json |ConvertTo-Hashtable |ConvertTo-Json

    {

    "ADGroup":  {

    "value":  [

    {

    "Scope":  "DomainLocal",

    "Name":  "mygroup",

    "Members":  {

    "value":  "System.Collections.Hashtable",

    "Count":  1

    },

    "DnsDomainName":  "lab.local"

    }

    ],

    "Count":  1

    },

    "SQLDatabase":  {

    "value":  [

    {

    "Name":  "FOO",

    "Ensure":  "Present",

    "Type":  "OnPrem"

    }

    ],

    "Count":  1

    },

    "ADUser":  {

    "value":  [

    {

    "DnsDomainName":  "lab.local",

    "Name":  "jjones"

    }

    ],

    "Count":  1

    }

    }

    0

Leave a reply

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

*

© 4sysops 2006 - 2019

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account