{"id":72881,"date":"2015-10-08T00:01:00","date_gmt":"2015-10-08T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/10\/08\/playing-with-json-and-powershell\/"},"modified":"2019-02-18T09:34:58","modified_gmt":"2019-02-18T16:34:58","slug":"playing-with-json-and-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/playing-with-json-and-powershell\/","title":{"rendered":"Playing with JSON and PowerShell"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about playing with JSON and Windows PowerShell&nbsp;5.0.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife has an updated shopping list. It includes a Microsoft Band&nbsp;2 and a Surface Pro&nbsp;4. The launch was a mouthwatering event and really well done.<\/p>\n<p>One of the cool cmdlets in Windows PowerShell&nbsp;5.0 on Windows 10 is the <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/hh849898.aspx\" target=\"_blank\">ConvertFrom-JSON cmdlet<\/a>. One reason it is cool is that it will convert a <a href=\"https:\/\/en.wikipedia.org\/wiki\/JSON\" target=\"_blank\">Java Script Object Notation (JSON)<\/a> string into a custom Windows PowerShell object. This is a cool way to interact with web services, and it can save a bit of time from parsing XML. In fact, it is remarkably easy to do.<\/p>\n<h2>The band, man, the band!<\/h2>\n<p>As an example, I am going to use a beta JSON interface to the MusicBrainz web service. This permits me to use <b>Invoke-WebRequest<\/b> and return a JSON formatted string.<\/p>\n<p>The following request performs an artist lookup and returns a description, name, and country of origin (in addition to other stuff) about the artist. The query contains the website, the API, the artist information, and the format requested. Here is the query string:<\/p>\n<p style=\"margin-left:30px\">$request = &#039;http:\/\/musicbrainz.org\/ws\/2\/artist\/5b11f4ce-a62d-471e-81fc-a69a8278c7da?inc=aliases&amp;fmt=json&#039;<\/p>\n<p>To execute this query is simple, I use Invoke-WebRequest. This appears here:<\/p>\n<p style=\"margin-left:30px\">Invoke-WebRequest $request<\/p>\n<p>Because I requested the data to return as JSON, I need to convert it from JSON. I use the new <b>ConvertFrom-JSON<\/b> cmdlet and select the three fields I am interested in looking at:<\/p>\n<p style=\"margin-left:30px\">ConvertFrom-Json |<\/p>\n<p style=\"margin-left:30px\">Select name, disambiguation, country<\/p>\n<p>The script, and the output from the script are shown here:<\/p>\n<p style=\"margin-left:30px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-10-8-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-10-8-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If I did not convert from JSON, the output would look like this:<\/p>\n<p style=\"margin-left:30px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-10-8-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-10-8-15-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The returned and the converted from JSON output are shown here:<\/p>\n<p style=\"margin-left:30px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0312.hsg-10-8-15-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0312.hsg-10-8-15-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I can also find specific information about recordings by querying the <b>Recording<\/b> interface. The procedure is exactly the same. I store my query string, use <b>Invoke-WebRequest<\/b>, convert the output to JSON, and select the properties I am interested in.<\/p>\n<p>Because the properties about the recording I am interested in are stored in the <b>Releases<\/b> property as embedded objects, I need to expand that property, and then choose what I want from the embedded object. Here is that code:<\/p>\n<p style=\"margin-left:30px\">$request = &#039;http:\/\/musicbrainz.org\/ws\/2\/recording\/fcbcdc39-8851-4efc-a02a-ab0e13be224f?inc=artist-credits+isrcs+releases&amp;fmt=json&#039;<\/p>\n<p style=\"margin-left:30px\">Invoke-WebRequest $request |<\/p>\n<p style=\"margin-left:30px\">ConvertFrom-Json&nbsp; |<\/p>\n<p style=\"margin-left:30px\">select -expand releases |<\/p>\n<p style=\"margin-left:30px\">Select title, date, country<\/p>\n<p>The script and the output are shown here:<\/p>\n<p style=\"margin-left:30px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-10-8-15-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-10-8-15-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>There are other sample queries on the <a href=\"http:\/\/musicbrainz.org\/doc\/Development\/JSON_Web_Service\" target=\"_blank\">MusicBrainz.Org JSON<\/a> site that you can play around with to get familiar with parsing JSON. It is fun, interesting, and pretty easy to do&mdash;great combination, if you ask me.<\/p>\n<p>That is all there is to using a web request and returning JSON. Join me tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about playing with JSON and Windows PowerShell&nbsp;5.0. Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife has an updated shopping list. It includes a Microsoft Band&nbsp;2 and a Surface Pro&nbsp;4. The launch was a mouthwatering event and really well done. One of the cool cmdlets in Windows [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[621,609,3,4,604,45],"class_list":["post-72881","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-json","tag-powershell-5","tag-scripting-guy","tag-scripting-techniques","tag-using-the-web","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about playing with JSON and Windows PowerShell&nbsp;5.0. Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife has an updated shopping list. It includes a Microsoft Band&nbsp;2 and a Surface Pro&nbsp;4. The launch was a mouthwatering event and really well done. One of the cool cmdlets in Windows [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/72881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=72881"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/72881\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=72881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=72881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=72881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}