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!

0 comments… add one

Leave a Comment

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