Building nested json from powershell objects

I’ve been working on a pretty large project in powershell that involves a ton of REST API integration. Most APIs these days use nested json, building json in powershell is actually pretty easy but its not as clear cut as building xml (at least it wasnt for me). Lets take a look at how we can build a very simple example, and then end with something more complex.

First you need to understand json, if you dont take a look at this post:

Ok lets look at a simple example:

How can we get this generated in powershell?
Well one way is the manual way like this:

As you can tell this will output the raw json and then an object containing the json elements you made. This completely works but has its limitations, its not dynamic and it looks kinda gross in code when you start getting large json files. So how can we make it better? Lets use powershell objects and see what we can do:

Cool, so this shows that we created a hashtable and then converted that hashtable to json. Thats a pretty simple example though, lets step up to something more dynamic and nested.

Lets start with the actual json doc we want to create in code:

Ok, so this created an object for us with nested elements, the question becomes how can we build this from code? Well the first step is to build the objects that are the furthest in the tree first. So lets build out networking and systems objects:

Now that we have our two objects that are nested, lets build the root tree now:

Ok awesome so now we are generating nested json from code. But whats happening? In short we built a custom object that is ordered, we then added the elements needed and also added the nested objects we built to the proper elements.

Now obviously we are missing some of this solution, your inputs for both systems and networking would be a foreach loop or a pre-built object or similar to get that data into those variables but this is 90% of the work. I hope this helps building json in powershell!

13 comments… add one
  • Zehra Link Reply

    Thank you for this post. Solves the problem of arrays turning into hashtables when you insert a hashtable.

  • Byron Link Reply

    Very nice explanation here, and easy understand example. Thanks

  • If Link Reply

    Thanks this helps heaps

  • Mark Sadler Link Reply

    This was a great example for me to follow – especially since it was the first time constructing JSON in PowerShell. Thanks for that; however, it was missing one very crucial piece: the depth parameter for ConvertTo-Json. It’s taken me 6 hours of studying this post, trial/error and searching to figure out this missing piece buried in the comments section of another post. In your example above, JSON only goes 2 levels deep. Ironically, the other post indicated that “2” is the default depth level. In my case, the JSON I needed to create went 3 levels deep. Once I added the depth parameter, it constructed the JSON they way I needed. Here’s my line:

    $UpdBody = $Body| ConvertTo-Json -Depth 10

  • Tim Link Reply

    Thank you for your post, I had been banging my head against a wall trying to get the nested hash table to stay when converting to JSON.

  • Ranjan Link Reply

    Thank you for the post. It really helped alot.

  • Faris Link Reply

    Great, But what if you have another array in an array
    Such as
    “networking”: [
    “externalDns”: “testclient.external.lan”,
    “Interface”: [
    “Interface1”: “MyLocalLan”,
    “Interface2”: “MyExternalLan”,
    “externalDns”: “testclientuat.external.lan”,
    “externalIp”: “”

    • That is not valid json. You have an array with a hash key value pair. Either way its the exact same concept thats shown throughout this post but applied to this field.

  • Alex Link Reply

    Lol, damn, your code, pasted in a PS file, works like a charm!!
    My code (foreach loop to generate sub elements) keep resulting in a “convertto-json” file enclosed into square brackets and sub element in @[……..] form :\

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.