{"id":9602,"date":"2024-05-08T07:03:46","date_gmt":"2024-05-08T07:03:46","guid":{"rendered":"https:\/\/cheapwindowsvps.com\/blog\/guide-to-reading-modifying-and-parsing-json-files-with-powershell\/"},"modified":"2025-01-20T09:38:03","modified_gmt":"2025-01-20T09:38:03","slug":"guide-to-reading-modifying-and-parsing-json-files-with-powershell","status":"publish","type":"post","link":"https:\/\/cheapwindowsvps.com\/blog\/guide-to-reading-modifying-and-parsing-json-files-with-powershell\/","title":{"rendered":"Guide to Reading, Modifying, and Parsing JSON Files with PowerShell"},"content":{"rendered":"<p><p>JSON is a popular text-based format for representing and transmitting structured data based on JavaScript object syntax. There are two cmdlets in PowerShell that allow you to work with the JSON data format: <code>ConvertFrom-Json<\/code> and <code>ConvertTo-Json<\/code>. Let\u2019s look at how you can use PowerShell to create, read, or modify JSON objects and save them to files.<\/p>\n<\/p>\n<p><p>Data in JSON format is represented as <strong>key:value<\/strong> pairs (property nesting is allowed). Suppose you want to write JSON data to a file. Create a JSON structured data object:<\/p>\n<\/p>\n<p><pre><\/p><p>$obj = @{<\/p><p>    Name = \"Henry\"<\/p><p>    Roles = @{<\/p><p>          AD = \"Admin\"<\/p><p>          SQL = \"Report\"<\/p><p>      }<\/p><p>      \"Company\" = \"woshub\"<\/p><p>  }<\/pre>\n<\/p>\n<p><p>Now convert the object to JSON format and save it to a file with a <strong>.json<\/strong> extension:<\/p>\n<\/p>\n<p><pre><code>$json = $obj | ConvertTo-Json<\/p><p>$json | Set-Content -Path C:PSuserroles.json<\/code><\/pre>\n<\/p>\n<p><p>You can now read the JSON file:<\/p>\n<\/p>\n<p><pre><code>$json = Get-Content -Path C:PSuserroles.json -Raw | ConvertFrom-Json<\/code><\/pre>\n<\/p>\n<div>\n<\/div>\n<p><p>List all JSON object properties:<\/p>\n<\/p>\n<p><p><code>$json|fl<\/code><\/p>\n<\/p>\n<p><p>Or you can get the value of a particular property in a JSON object:<\/p>\n<\/p>\n<p><p><code>$json.roles.sql<\/code><\/p>\n<\/p>\n<p><p>Use the <strong>Add-Member<\/strong> command to add a new property to a JSON object:<\/p>\n<\/p>\n<p><p><code>$json| Add-Member -MemberType NoteProperty -Name \"Email\" -Value \"<a href=\"\/cdn-cgi\/l\/email-protection\" rel=\"nofollow noopener\" target=\"_blank\">[email&nbsp;protected]<\/a>\"<\/code><\/p>\n<\/p>\n<p><p>Use the following commands to change a single value in a JSON object and save it to a file:<\/p>\n<\/p>\n<p><pre><\/p><p>&lt;code&gt;<\/p><p>$json.roles.sql='Admin';<\/p><p>$json|ConvertTo-Json| Set-Content -Path C:PSuserroles.json<\/p><p>&lt;\/code&gt;<\/p><p><\/pre>\n<\/p>\n<p><p>Remove JSON object property:<\/p>\n<\/p>\n<p><pre><\/p><p>&lt;code&gt;<\/p><p>$json.PSObject.Properties.Remove(\"Email\")<\/p><p>&lt;\/code&gt;<\/p><p><\/pre>\n<\/p>\n<p><p>By utilizing the <a href=\"https:\/\/woshub.com\/parsing-html-webpages-with-powershell\/\" rel=\"nofollow noopener\" target=\"_blank\">Invoke-WebRequest<\/a> PowerShell command, JSON HTTP API data can be retrieved from external web services or websites. As an example, A records returned by Google&#8217;s DNS service in JSON format can be listed:<\/p>\n<\/p>\n<p><p><code>$site=\"woshub.com\"<br \/> $rawresp=Invoke-WebRequest \"https:\/\/dns.google\/resolve?name=$site&amp;type=A\"<br \/> $rawjson = ConvertFrom-Json -InputObject $rawresp.Content<br \/> $rawjson.answer.data<\/code><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON is a popular text-based format for representing and transmitting structured data based on JavaScript object syntax. There are two cmdlets in PowerShell that allow you to work with the JSON data format: ConvertFrom-Json and ConvertTo-Json. Let\u2019s look at how you can use PowerShell to create, read, or modify JSON objects and save them to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9603,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117],"tags":[],"class_list":["post-9602","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/comments?post=9602"}],"version-history":[{"count":2,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9602\/revisions"}],"predecessor-version":[{"id":10298,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9602\/revisions\/10298"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media\/9603"}],"wp:attachment":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media?parent=9602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/categories?post=9602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/tags?post=9602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}