How to Edit a JSON File in PowerShell (With Example) - Collecting Wisdom (2024)

Posted on by Zach

Often you may want to edit the values in a JSON file using PowerShell.

Here is an example of how to do so:

$file = Get-Content my_teams.json -raw | ConvertFrom-Json$file | % {if($_.Location -eq 'Dallas'){$_.Name='Mavs'}}$file | ConvertTo-Json -depth 32 > new_teams.json

Here is what this particular example does:

  • First, retrieve the JSON file named my_teams.json
  • Next, for each value in the Location field of the file that is equal to ‘Dallas’, update the corresponding value in the Name field to be equal to ‘Mavs’
  • Lastly, output these changes to a new JSON file named new_teams.json

Note: The default value for depth when using ConvertTo-Json is 2, but we specify a depth of 32 in this example to avoid any issues that may arise from not having a deeply enough nested JSON.

The following example shows how to use this syntax in practice.

Example: How to Edit a JSON File in PowerShell

Suppose that we have a JSON file named my_teams.json with information about various basketball teams.

We can use the following syntax to view the contents of this file:

Get-Content my_teams.json

The following screenshot shows how to use this syntax in practice:

How to Edit a JSON File in PowerShell (With Example) - Collecting Wisdom (1)

Suppose that we would like to find all values in the Location field of the file that are equal to ‘Dallas’ and change the corresponding value in the Name field to be ‘Mavs’ instead.

We can use the following syntax to do so:

$file = Get-Content my_teams.json -raw | ConvertFrom-Json$file | % {if($_.Location -eq 'Dallas'){$_.Name='Mavs'}}$file | ConvertTo-Json -depth 32 > new_teams.json

The following screenshot shows how to use this syntax in practice:

How to Edit a JSON File in PowerShell (With Example) - Collecting Wisdom (2)

Notice that when we use the Get-Content cmdlet to view the contents of the new file named new_teams.json we can see that ‘Mavericks’ has been renamed to ‘Mavs’ in the first Name field of the file.

Note: You can find the complete documentation for the ConvertTo-Jsoncmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Sort a Table with Format-Table
PowerShell: How to Sort by Specific Column
PowerShell: How to Use Sort-Object with Multiple Properties

How to Edit a JSON File in PowerShell (With Example) - Collecting Wisdom (2024)
Top Articles
Canceling eBay Listing The Easiest Way
When is the Best Time to Buy a House in California?
How To Start a Consignment Shop in 12 Steps (2024) - Shopify
Pinellas County Jail Mugshots 2023
Weather Annapolis 10 Day
LA Times Studios Partners With ABC News on Randall Emmett Doc Amid #Scandoval Controversy
อพาร์ทเมนต์ 2 ห้องนอนในเกาะโคเปนเฮเกน
No Strings Attached 123Movies
My.tcctrack
Where to Find Scavs in Customs in Escape from Tarkov
Energy Healing Conference Utah
Epguides Strange New Worlds
Robin D Bullock Family Photos
The best firm mattress 2024, approved by sleep experts
Mc Donald's Bruck - Fast-Food-Restaurant
Busted Mcpherson Newspaper
Little Rock Skipthegames
About My Father Showtimes Near Copper Creek 9
PCM.daily - Discussion Forum: Classique du Grand Duché
R&S Auto Lockridge Iowa
Riversweeps Admin Login
Troy Gamefarm Prices
Bento - A link in bio, but rich and beautiful.
Breckiehill Shower Cucumber
What Equals 16
Ullu Coupon Code
Penn State Service Management
Pioneer Library Overdrive
Datingscout Wantmatures
Bad Business Private Server Commands
Rubmaps H
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Roadtoutopiasweepstakes.con
Western Gold Gateway
Aliciabibs
Los Garroberros Menu
Td Ameritrade Learning Center
Taylor University Baseball Roster
Shuaiby Kill Twitter
Let's co-sleep on it: How I became the mom I swore I'd never be
Henry Ford’s Greatest Achievements and Inventions - World History Edu
Pro-Ject’s T2 Super Phono Turntable Is a Super Performer, and It’s a Super Bargain Too
Directions To The Closest Auto Parts Store
Tgirls Philly
Stranahan Theater Dress Code
Jamesbonchai
Minterns German Shepherds
Craigslist Pets Charleston Wv
Steam Input Per Game Setting
Sj Craigs
Ok-Selection9999
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 6172

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.