How To Replace String In JSON File Using PowerShell?

    Recently, I got a requirement to replace a string in a JSON file. PowerShell provides different cmdlets to work with JSON files. In this tutorial, I will explain how to replace a string in a json file using PowerShell.

    To replace a string in a JSON file using PowerShell, you would read the JSON file into a PowerShell object, modify the string, convert the object back to JSON, and then save the changes back to the file. For example, to change the version number in a config.json file, you would use Get-Content to read the file, update the version property, and then use Set-Content to write the updated JSON back to the file.

    Replace String In JSON File Using PowerShell

    Here’s a complete tutorial on how to replace a string in a JSON file using PowerShell.

    Before you begin, ensure that you have a JSON file that you want to modify. For this example, let’s assume you have a file named config.json with the following content:

    {
        "name": "SampleApp",
        "version": "1.0",
        "description": "This is a sample configuration file."
    }

    Suppose you want to update the version value from 1.0 to 2.0.

    Step 1: Read the JSON File

    First, you need to read the content of the JSON file using Get-Content cmdlet and convert it from JSON into a PowerShell object.

    $jsonContent = Get-Content 'config.json' -Raw | ConvertFrom-Json

    Step 2: Modify the Desired Value

    Next, modify the version property of the object.

    $jsonContent.version = "2.0"

    Step 3: Convert Back to JSON

    Convert the modified object back to a JSON string. You can use the ConvertTo-Json cmdlet for this purpose.

    $jsonString = $jsonContent | ConvertTo-Json

    Step 4: Save the Changes

    Finally, save the updated JSON string back to the file using Set-Content.

    Set-Content 'config.json' -Value $jsonString

    Complete PowerShell Script

    Here is the complete script that combines all the steps above:

    # Step 1: Read the JSON file into a PowerShell object
    $jsonContent = Get-Content 'config.json' -Raw | ConvertFrom-Json
    
    # Step 2: Modify the 'version' property
    $jsonContent.version = "2.0"
    
    # Step 3: Convert the object back to a JSON string
    $jsonString = $jsonContent | ConvertTo-Json
    
    # Step 4: Save the updated JSON string back to the file
    Set-Content 'config.json' -Value $jsonString

    Running the Script

    To run the script, simply save it with a .ps1 extension, for example, UpdateJsonVersion.ps1. Then, you can execute this script in PowerShell by navigating to the directory containing the script and running:

    .\UpdateJsonVersion.ps1

    Make sure to run PowerShell with the necessary permissions to read and write to the file location.

    This script will update the config.json file’s version property to 2.0. You can adapt this script to change other properties or work with different JSON structures by modifying the property paths accordingly.

    Following these steps, you can easily replace a string in a JSON file using PowerShell.

    Conclusion

    In this PowerShell tutorial, I have explained how to replace a string in a JSON file with PowerShell.

    You may also like:

    >