Windows OS Hub
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server 2025
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
    • Proxmox
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows 10
    • Windows Server 2025
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
    • Proxmox
  • PowerShell
  • Linux

 Windows OS Hub / PowerShell / Read, Modify, and Parse JSON File (Object) with PowerShell

May 8, 2024

Read, Modify, and Parse JSON File (Object) with PowerShell

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’s look at how you can use PowerShell to create, read, or modify JSON objects and save them to files.

Data in JSON format is represented as key:value pairs (property nesting is allowed). Suppose you want to write JSON data to a file. Create a JSON structured data object:

$obj = @{
    Name = "Henry"
    Roles = @{
          AD = "Admin"
          SQL = "Report"
      }
      "Company" = "woshub"
  }

Now convert the object to JSON format and save it to a file with a .json extension:

$json = $obj | ConvertTo-Json
$json | Set-Content -Path C:\PS\userroles.json

You can now read the JSON file:

$json = Get-Content -Path C:\PS\userroles.json -Raw | ConvertFrom-Json

List all JSON object properties:

$json|fl

Or you can get the value of a particular property in a JSON object:

$json.roles.sql

Use the Add-Member command to add a new property to a JSON object:

$json| Add-Member -MemberType NoteProperty -Name "Email" -Value "[email protected]"

modify JSON object, add a property with PowerShell

Use the following commands to change a single value in a JSON object and save it to a file:

$json.roles.sql='Admin'
$json|ConvertTo-Json| Set-Content -Path C:\PS\userroles.json

Remove JSON object property:

$json.PSObject.Properties.Remove("Email")

By using the Invoke-WebRequest PowerShell cmdlet, you can access the JSON HTTP API to get data from external web services (sites). For example, to list A records returned by Google’s DNS service in JSON format:

$site="woshub.com"
$rawresp=Invoke-WebRequest "https://dns.google/resolve?name=$site&type=A"
$rawjson = ConvertFrom-Json -InputObject $rawresp.Content
$rawjson.answer.data

PowerShell: read JSON data from webservice

0 comment
1
Facebook Twitter Google + Pinterest
PowerShell
previous post
Configure DNS Scavenging to Clean Up Stale DNS Records in AD
next post
Create a Multi-OS Bootable USB Flash Drive with Ventoy

Related Reading

How to Install Remote Server Administration Tools (RSAT)...

March 17, 2024

Fix: Remote Desktop Licensing Mode is not Configured

August 24, 2023

Managing Windows Firewall Rules with PowerShell

March 11, 2024

How to Allow Non-Admin User to Start/Stop Service...

March 15, 2024

Installing Language Pack in Windows 10/11 with PowerShell

September 20, 2023

How to Assign (Passthrough) a Physical GPU to...

June 11, 2024

Extend an Expired User Password in Active Directory

December 23, 2024

How to Create UEFI Bootable USB Drive to...

March 13, 2024

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Find a Process Causing High Disk Usage on Windows

    July 15, 2025
  • Fix: Microsoft Defender Not Updating Automatically in Windows

    July 8, 2025
  • Create a Windows Server VM on Proxmox (Step-by-Step)

    July 7, 2025
  • How to Detect Which User Installed or Removed a Program on Windows

    June 23, 2025
  • Encrypt Any Client-Server App Traffic on Windows with Stunnel

    June 12, 2025
  • Failed to Open the Group Policy Object on a Computer

    June 2, 2025
  • Remote Desktop Printing with RD Easy Print Redirection

    June 2, 2025
  • Disable the Lock Screen Widgets in Windows 11

    May 26, 2025
  • Configuring Windows Protected Print Mode (WPP)

    May 19, 2025
  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • How to Delete Old User Profiles in Windows
  • Fix: Remote Desktop Licensing Mode is not Configured
  • How to Install Remote Server Administration Tools (RSAT) on Windows
  • How to Create UEFI Bootable USB Drive to Install Windows
  • Configuring User Profile Disks (UPD) on Windows Server RDS
  • Wi-Fi (Internet) Disconnects After Sleep or Hibernation on Windows 10/11
  • How to Allow Non-Admin User to Start/Stop Service in Windows
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top