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.

json3

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

Read about how I work with Graph through PowerShell here.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s