Why I did a major facepalm when I learned how easy JSON in PowerShell really is!

graph

You learn something new every day. For months I have been doing Graph API calls trough PowerShell with JSON requests. If you copy/paste a JSON request body right into your favorite PowerShell editor you will get a lot of errors.

json1

To have a request that your editor can understand you need to do it like this:

json2.png

But now you have to convert this to JSON again when doing your request.

$Body | ConvertTo-JSON

There was a lot of requests I was not able to convert, and that stopped me from creating the scripts I wanted.  So when I hit the wall with my Graph/PowerShell scripting, I took an extra thorough look through Goole.com and found something that instantly made me smile and feel joy. How could I not have noticed this before?

What you need to do to put a JSON request into a variable? You wrap it with @’ on the first line and ‘@ on the last. You can also use double quotes @” “@, then you can also put variables inside your request.

json3

Now you don’t even need to use ConvertTo-JSON!

Read about how I work with Graph through PowerShell here.

 

 

Leave a comment