{"componentChunkName":"component---src-templates-articles-index-jsx","path":"/articles/using-powershell-with-json-data/","result":{"data":{"props":{"adsense":true,"adsenseMobile":null,"byLine":"","canonical":"https://staging.business.com/articles/using-powershell-with-json-data/","content":"\n<p>JSON is a way of structuring data that makes it easy for software to consume. When working with PowerShell, Microsoft has provided some helpful tools to work with JSON called the ConvertTo-Json and ConvertFrom-Json commands.</p>\r\n<p>As the world continually becomes \"<a href=\"http://www.wsj.com/articles/SB10001424053111903480904576512250915629460\" target=\"_blank\">eaten by software</a>\" more and more services are being replaced by software. IT pros have most likely seen this in the form of software-defined everything. One of the premier components of this focus on software and with the continuing adoption of DevOps is application programming interfaces (APIs). All of these services needs to talk together and must provide a way for programs and users to interact with them. This is where APIs come in handy. But, what does this have to do with PowerShell and JSON, you ask?</p>\r\n<p>APIs, more specifically REST APIs, return data when queried. This data is typically in the <a href=\"http://www.w3schools.com/js/js_json_intro.asp\" target=\"_blank\">JSON</a> format. JSON is a way of structuring data that makes it easy for software to consume. When working with PowerShell, Microsoft has provided some helpful tools to work with JSON called the ConvertTo-Json and ConvertFrom-Json commands. These commands allow you to quickly work with REST APIs or any other service that returns or accepts JSON as an input.</p>\r\n<div id=\"in-article-vca-1\"> </div>\r\n<h2>How these two commands can be used to work with JSON.</h2>\r\n<p>Let's first start out with a simple piece of JSON to work with. We'll pretend that this might have been returned from a REST API or some other service.<br>{\"employees\":[<br>    {\"firstName\":\"John\", \"lastName\":\"Doe\"},<br>    {\"firstName\":\"Anna\", \"lastName\":\"Smith\"},<br>    {\"firstName\":\"Peter\", \"lastName\":\"Jones\"}<br>]}</p>\r\n<p>This example shows an employees object with an array of three employees inside. Let's now assign this to a string variable in PowerShell.<br>$employees = '{\"employees\":[<br>    {\"firstName\":\"John\", \"lastName\":\"Doe\"},<br>    {\"firstName\":\"Anna\", \"lastName\":\"Smith\"},<br>    {\"firstName\":\"Peter\", \"lastName\":\"Jones\"}<br>]}'</p>\r\n<p>If just reading this as a string there's not much we can do with it.</p>\r\n<p><picture class=\"aligncenter full\"><source srcset=\"/images/content/5b6/88a0151ee47c5588b4592/1200_-289- 1200w, /images/content/5b6/88a0151ee47c5588b4592/668_-160- 668w, /images/content/5b6/88a0151ee47c5588b4592/320_-77- 320w\" sizes=\"668px, 100%\"></source><img src=\"/images/content/5b6/88a0151ee47c5588b4592/930-224-\" alt=\"\" width=\"930\" height=\"224\" data-content-img-id=\"5b688a0151ee47c5588b4592\"></picture></p>\r\n<p>There's no way to pull out specific employees without doing a lot of string manipulation which can get ugly quick. Instead, we need to get this piece of JSON data into a structured object. After all, objects are what make PowerShell great. To do that, we can use the ConvertFrom-Json command and pass $employees to it.</p>\r\n<p><picture class=\"aligncenter full\"><source srcset=\"/images/content/5b6/88a2151ee47565b8b4590/1200_-304- 1200w, /images/content/5b6/88a2151ee47565b8b4590/668_-169- 668w, /images/content/5b6/88a2151ee47565b8b4590/320_-81- 320w\" sizes=\"668px, 100%\"></source><img src=\"/images/content/5b6/88a2151ee47565b8b4590/946-240-\" alt=\"\" width=\"946\" height=\"240\" data-content-img-id=\"5b688a2151ee47565b8b4590\"></picture></p>\r\n<p>Now you can see that this is an object that can more easily be parsed. At this point, we perform tasks on each employee like checking for an Active Directory perhaps.<br>$employeesObj.employees | foreach { $emp = $_; Get-AdUser -Filter \"(givenName -eq '$($emp.firstName)') -and (surName -eq '$($emp.lastName)')\" }</p>\r\n<h2>ConvertFrom-Json</h2>\r\n<p>The ConvertFrom-Json command is an excellent way to convert any piece of JSON directly into a manageable PowerShell object. But we can also go the other way as well. What if you have an object that you'd like to send to a REST API or some other service that requires the data be in JSON. We can use the ConvertTo-Json command.</p>\r\n<p>To use the ConvertTo-Json command. This does the exact opposite of ConvertFrom-Json. This command takes an object and converts it back into that JSON string we started with. Although, in this form, it's not possible to work with it in PowerShell, but that's not really the point. This command is typically used when the JSON is done being manipulated and is ready to be sent in pure JSON.</p>\r\n<p>Below you can see that by piping our object to the ConvertTo-Json command, that it reads the object and returns the exact same JSON string that was provided in the first place.</p>\r\n<p><picture class=\"aligncenter full\"><source srcset=\"/images/content/5b6/88a4092a3a4925c8b4595/1200_-458- 1200w, /images/content/5b6/88a4092a3a4925c8b4595/668_-255- 668w, /images/content/5b6/88a4092a3a4925c8b4595/320_-122- 320w\" sizes=\"668px, 100%\"></source><img src=\"/images/content/5b6/88a4092a3a4925c8b4595/952-364-\" alt=\"\" width=\"952\" height=\"364\" data-content-img-id=\"5b688a4092a3a4925c8b4595\"></picture></p>\r\n<p>The ConvertTo-Json and ConvertFrom-Json commands will probably be the only commands necessary to work with JSON on PowerShell. There are some more advanced scenarios that you might find in but, in that case, begin exploring each commands various parameters as we just used a single parameter in our examples above called InputObject.</p>\r\n<div id=\"in-article-vca-2\"> </div>\n","docType":"article","guid":"articles_7070f11064be3c609ca8f749528c85cf","lastModified":"2018-09-04T07:01:00.000Z","published":"2018-09-04T07:00:00.000Z","structuredData":null,"subtitle":"When REST APIs return data in JSON format, you can get at it through PowerShell.","template":"v1","title":"How to Use PowerShell to Work with JSON Data","type":"article","author":{"avatar":null,"bio":"Adam Bertram is a 20-year veteran of IT and experienced online business professional. He's an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn, or follow him on Twitter at @adbertram or the TechSnips Twitter account at @techsnips_io.","name":"Adam Bertram","image":null,"slug":null,"social":{"facebook":"","linkedin":"https://www.linkedin.com/in/adambertram/","twitter":"https://twitter.com/adbertram","website":"https://www.adamtheautomator.com/"},"status":"enabled","title":null,"type":"Contributing Writer","user":{"imagePath":"https://staging.business.com/images/undefined","profile":{"company":"eGain Communications","jobTitle":"Product Management Analyst","website":""}}},"breadcrumbs":null,"channels":{"primary":{"name":"Technology","slug":"technology"},"sub":{"name":"Technology Solutions","slug":"technology-solutions"}},"meta":{"robots":"index, follow","description":"When REST APIs return data in JSON format, you can get at it through PowerShell.","title":"How to Use PowerShell to Work with JSON Data - business.com"},"parsedContent":{"toc":[{"id":"how-these-two-commands-can-be-used-to-work-with-json","title":"How these two commands can be used to work with JSON."},{"id":"convertfrom-json","title":"ConvertFrom-Json"}],"json":[{"component":"p","children":["JSON is a way of structuring data that makes it easy for software to consume. When working with PowerShell, Microsoft has provided some helpful tools to work with JSON called the ConvertTo-Json and ConvertFrom-Json commands."]},{"component":"p","children":["As the world continually becomes \"",{"component":"a","props":{"href":"http://www.wsj.com/articles/SB10001424053111903480904576512250915629460","target":"_blank"},"children":["eaten by software"]},"\" more and more services are being replaced by software. IT pros have most likely seen this in the form of software-defined everything. One of the premier components of this focus on software and with the continuing adoption of DevOps is application programming interfaces (APIs). All of these services needs to talk together and must provide a way for programs and users to interact with them. This is where APIs come in handy. But, what does this have to do with PowerShell and JSON, you ask?"]},{"component":"p","children":["APIs, more specifically REST APIs, return data when queried. This data is typically in the ",{"component":"a","props":{"href":"http://www.w3schools.com/js/js_json_intro.asp","target":"_blank"},"children":["JSON"]}," format. JSON is a way of structuring data that makes it easy for software to consume. When working with PowerShell, Microsoft has provided some helpful tools to work with JSON called the ConvertTo-Json and ConvertFrom-Json commands. These commands allow you to quickly work with REST APIs or any other service that returns or accepts JSON as an input."]},{"component":"div","props":{"id":"in-article-vca-1"},"children":[" "]},{"component":"h2","props":{"id":"how-these-two-commands-can-be-used-to-work-with-json"},"children":["How these two commands can be used to work with JSON."]},{"component":"p","children":["Let's first start out with a simple piece of JSON to work with. We'll pretend that this might have been returned from a REST API or some other service.",{"component":"br"},"{\"employees\":[",{"component":"br"},"    {\"firstName\":\"John\", \"lastName\":\"Doe\"},",{"component":"br"},"    {\"firstName\":\"Anna\", \"lastName\":\"Smith\"},",{"component":"br"},"    {\"firstName\":\"Peter\", \"lastName\":\"Jones\"}",{"component":"br"},"]}"]},{"component":"p","children":["This example shows an employees object with an array of three employees inside. Let's now assign this to a string variable in PowerShell.",{"component":"br"},"$employees = '{\"employees\":[",{"component":"br"},"    {\"firstName\":\"John\", \"lastName\":\"Doe\"},",{"component":"br"},"    {\"firstName\":\"Anna\", \"lastName\":\"Smith\"},",{"component":"br"},"    {\"firstName\":\"Peter\", \"lastName\":\"Jones\"}",{"component":"br"},"]}'"]},{"component":"p","children":["If just reading this as a string there's not much we can do with it."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter full"},"children":[{"component":"source","props":{"srcSet":"/images/content/5b6/88a0151ee47c5588b4592/1200_-289- 1200w, /images/content/5b6/88a0151ee47c5588b4592/668_-160- 668w, /images/content/5b6/88a0151ee47c5588b4592/320_-77- 320w","sizes":"668px, 100%"}},{"component":"img","props":{"src":"/images/content/5b6/88a0151ee47c5588b4592/930-224-","alt":"","width":"930","height":"224","data-content-img-id":"5b688a0151ee47c5588b4592"}}]}]},{"component":"p","children":["There's no way to pull out specific employees without doing a lot of string manipulation which can get ugly quick. Instead, we need to get this piece of JSON data into a structured object. After all, objects are what make PowerShell great. To do that, we can use the ConvertFrom-Json command and pass $employees to it."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter full"},"children":[{"component":"source","props":{"srcSet":"/images/content/5b6/88a2151ee47565b8b4590/1200_-304- 1200w, /images/content/5b6/88a2151ee47565b8b4590/668_-169- 668w, /images/content/5b6/88a2151ee47565b8b4590/320_-81- 320w","sizes":"668px, 100%"}},{"component":"img","props":{"src":"/images/content/5b6/88a2151ee47565b8b4590/946-240-","alt":"","width":"946","height":"240","data-content-img-id":"5b688a2151ee47565b8b4590"}}]}]},{"component":"p","children":["Now you can see that this is an object that can more easily be parsed. At this point, we perform tasks on each employee like checking for an Active Directory perhaps.",{"component":"br"},"$employeesObj.employees | foreach { $emp = $_; Get-AdUser -Filter \"(givenName -eq '$($emp.firstName)') -and (surName -eq '$($emp.lastName)')\" }"]},{"component":"h2","props":{"id":"convertfrom-json"},"children":["ConvertFrom-Json"]},{"component":"p","children":["The ConvertFrom-Json command is an excellent way to convert any piece of JSON directly into a manageable PowerShell object. But we can also go the other way as well. What if you have an object that you'd like to send to a REST API or some other service that requires the data be in JSON. We can use the ConvertTo-Json command."]},{"component":"p","children":["To use the ConvertTo-Json command. This does the exact opposite of ConvertFrom-Json. This command takes an object and converts it back into that JSON string we started with. Although, in this form, it's not possible to work with it in PowerShell, but that's not really the point. This command is typically used when the JSON is done being manipulated and is ready to be sent in pure JSON."]},{"component":"p","children":["Below you can see that by piping our object to the ConvertTo-Json command, that it reads the object and returns the exact same JSON string that was provided in the first place."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter full"},"children":[{"component":"source","props":{"srcSet":"/images/content/5b6/88a4092a3a4925c8b4595/1200_-458- 1200w, /images/content/5b6/88a4092a3a4925c8b4595/668_-255- 668w, /images/content/5b6/88a4092a3a4925c8b4595/320_-122- 320w","sizes":"668px, 100%"}},{"component":"img","props":{"src":"/images/content/5b6/88a4092a3a4925c8b4595/952-364-","alt":"","width":"952","height":"364","data-content-img-id":"5b688a4092a3a4925c8b4595"}}]}]},{"component":"p","children":["The ConvertTo-Json and ConvertFrom-Json commands will probably be the only commands necessary to work with JSON on PowerShell. There are some more advanced scenarios that you might find in but, in that case, begin exploring each commands various parameters as we just used a single parameter in our examples above called InputObject."]},{"component":"div","props":{"id":"in-article-vca-2"},"children":[" "]}]},"resource":null,"sponsorship":null,"thumbnail":{"path":"https://staging.business.com/images/content/5db/c5acc9c658aaa778b457a/1500-0-","caption":"dusanpetkovic / Getty Images","isBlurred":false},"bestPicks":null},"related":{"relatedArticles":[{"title":"How to Use PowerShell to Work with JSON Data","canonical":"https://www.business.com/articles/using-powershell-with-json-data/","slug":"using-powershell-with-json-data","thumbnail":"https://www.business.com/images/content/5db/c5acc9c658aaa778b457a/1500-0-","dek":"When REST APIs return data in JSON format, you can get at it through PowerShell.","lastModified":"2018-09-04T07:01:00Z","published":"2018-09-04T07:00:00Z","sponsored":false},{"title":"PowerShell: Everything You Need to Know","canonical":"https://www.business.com/articles/powershell-everything-you-need-to-know/","slug":"powershell-everything-you-need-to-know","thumbnail":"https://www.business.com/images/content/5b8/998581039eb8f738b457b/1500-0-","dek":"What you need to know to unlock Microsoft PowerShell's full capabilities.","lastModified":"2018-09-05T17:00:00Z","published":"2018-09-05T17:00:00Z","sponsored":false},{"title":"From COVID-19 to Hurricane Season: Disaster Preparedness for Small Business","canonical":"https://www.business.com/articles/business-disaster-prep/","slug":"business-disaster-prep","thumbnail":"https://www.business.com/images/content/5e7/a495e5a215eb11d8b4569/1500-0-","dek":"Natural disasters – whether a hurricane, tornado or outbreak like COVID-19 – can have a disastrous effect on small businesses, but they don't have to. Here's how to weather a disaster successfully.","lastModified":"2020-03-25T15:19:00Z","published":"2018-09-26T07:00:00Z","sponsored":false},{"title":"Essential PowerShell Cmdlets For Managing Active Directory","canonical":"https://www.business.com/articles/powershell-active-directory-cmdlets/","slug":"powershell-active-directory-cmdlets","thumbnail":"https://www.business.com/images/content/5cf/90cab7b4374ab068b456f/1500-0-","dek":"Administering Active Directory is one of the most critical roles in any Windows network.","lastModified":"2018-09-04T08:00:00Z","published":"2018-09-04T08:00:00Z","sponsored":false},{"title":"How to Create a Web Scraping Tool in PowerShell","canonical":"https://www.business.com/articles/create-web-scraping-tool-in-powershell/","slug":"create-web-scraping-tool-in-powershell","thumbnail":"https://www.business.com/images/content/5d0/06c899c658acc288b458a/1500-0-","dek":"Write a tool in PowerShell that will gather all the data from a web page.","lastModified":"2018-09-05T09:00:00Z","published":"2018-09-05T09:00:00Z","sponsored":false},{"title":"Essential PowerShell Cmdlets For Auditing And Maintaining Storage","canonical":"https://www.business.com/articles/powershell-for-storage/","slug":"powershell-for-storage","thumbnail":"https://www.business.com/images/content/5d0/18e065a215ea2498b4588/1500-0-","dek":"PowerShell v5 offers some new features for managing storage.","lastModified":"2018-09-04T11:00:00Z","published":"2018-09-04T11:00:00Z","sponsored":false},{"title":"Using Lean Strategies to Achieve Team Synergy and Efficiency","canonical":"https://www.business.com/articles/lean-strategies-team-efficiency/","slug":"lean-strategies-team-efficiency","thumbnail":"https://www.business.com/images/content/5be/480e49c658a51108b458c/1500-0-","dek":"Learn more about lean manufacturing strategies and how to implement them in your business.","lastModified":"2018-11-08T08:00:00Z","published":"2018-11-08T08:00:00Z","sponsored":false},{"title":"Mobile Wallet Guide: Google Pay vs. Apple Pay vs. Samsung Pay - 08-05","canonical":"https://staging.business.com/articles/google-pay-vs-apple-pay-vs-samsung-pay/","slug":"google-pay-vs-apple-pay-vs-samsung-pay","thumbnail":"https://staging.business.com/images/content/5c5/afd7f54d26d67178b458d/1500-0-","dek":"Edit 07-24 - subtitle","lastModified":"2019-08-05T20:58:00Z","published":"2018-06-21T16:05:00Z","sponsored":null},{"title":"How to Hire for Your Business","canonical":"https://www.business.com/articles/how-to-staff-business/","slug":"how-to-staff-business","thumbnail":"https://www.business.com/images/content/5ea/9c8bb9c658aee018b456c/1500-0-","dek":"Sterling Staffing Solutions CEO Stephen Carter shares his tips on staffing your business.","lastModified":"2020-07-31T15:00:00Z","published":"2018-08-16T12:00:00Z","sponsored":false},{"title":"Mobile Wallet Guide: Google Pay vs. Apple Pay vs. Samsung Pay","canonical":"https://www.business.com/articles/google-pay-vs-apple-pay-vs-samsung-pay/","slug":"google-pay-vs-apple-pay-vs-samsung-pay","thumbnail":"https://www.business.com/images/content/5c5/e0a269c658a457c8b4591/1500-0-","dek":"You no longer need to carry a bulky wallet filled with numerous credit cards or cash.","lastModified":"2018-06-21T16:06:00Z","published":"2018-06-21T16:05:00Z","sponsored":false},{"title":"Use PowerShell to Make WSUS Suck Less","canonical":"https://www.business.com/articles/powershell-windows-software-update-services/","slug":"powershell-windows-software-update-services","thumbnail":"https://www.business.com/images/content/5b8/eca2d92a3a4ee5a8b4574/1500-0-","dek":"Windows Software Update Services can use PowerShell to work with servers and Windows.","lastModified":"2018-09-04T10:00:00Z","published":"2018-09-04T10:00:00Z","sponsored":false},{"title":"Your Guide to Creating a Small Business Marketing Plan","canonical":"https://www.business.com/articles/sample-marketing-plan-outline-and-template/","slug":"sample-marketing-plan-outline-and-template","thumbnail":"https://www.business.com/images/content/5f3/3e1765a215eb62d8b4567/1500-0-","dek":"Follow these templates and guidelines to get started on creating your business's marketing plan.","lastModified":"2020-08-12T12:28:00Z","published":"2018-03-02T13:00:00Z","sponsored":false}],"relatedReviews":[{"doc_type":"category","thumbnail":"https://staging.business.com/images/rz/5df908cc5a215e8c4a8b4568/conferencecall_Antonio_Diaz_Getty.jpg","canonical":"https://staging.business.com/categories/best-conference-call-services/","title":"The Best Conference Call Services of 2021","dek":"Which conference call service is right for your business in 2021? We review the top-rated services.","slug":"best-conference-call-services","lastModified":"2021-03-17T18:17:00Z"},{"doc_type":"category","thumbnail":"https://staging.business.com/images/rz/5df3d8695a215e58428b4570/POS_GETTY_DERO2084.jpg","canonical":"https://staging.business.com/categories/best-pos-systems/","title":"The Best POS Systems of 2021","dek":"Before you choose a POS system, check out our reviews of the best picks for 2021. ","slug":"best-pos-systems","lastModified":"2021-03-17T17:33:00Z"},{"doc_type":"category","thumbnail":"https://staging.business.com/images/rz/5e00d4787b43745d6a8b456e/Laptop_undrey_getty.jpg","canonical":"https://staging.business.com/categories/employee-monitoring-software/","title":"The Best Employee Monitoring Software of 2021","dek":"We've narrowed down the best employee monitoring software for businesses. Check out our best picks and reviews.","slug":"employee-monitoring-software","lastModified":"2021-03-05T15:40:00Z"}],"relatedSponsoredArticles":null}},"pageContext":{"displayModified":"2018-09-04T07:01:00.000Z","template":"v1","title":"How to Use PowerShell to Work with JSON Data","meta":{"robots":"index, follow"},"docType":"articles","slug":"using-powershell-with-json-data","canonical":"https://staging.business.com/articles/using-powershell-with-json-data/","skipTOC":false}},"staticQueryHashes":[]}