How to Dynamically Update JSON Values in PowerShell with Set-ByPath Functionality
Автор: vlogize
Загружено: 2025-04-01
Просмотров: 6
Learn how to effectively update JSON values in PowerShell using a dynamic approach with the `Set-ByPath` function. This guide will help you understand the process step-by-step.
---
This video is based on the question https://stackoverflow.com/q/70156784/ asked by the user 'AdamCodes716' ( https://stackoverflow.com/u/13382745/ ) and on the answer https://stackoverflow.com/a/70156951/ provided by the user 'Mathias R. Jessen' ( https://stackoverflow.com/u/712649/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Powershell: Trying to set value in json
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
How to Dynamically Update JSON Values in PowerShell with Set-ByPath Functionality
PowerShell is a powerful scripting language that provides users with easy ways to manage JSON files. However, one common challenge developers face is updating specific values in a JSON object dynamically. This guide will guide you through solving the problem of setting a value in a nested JSON structure when only a path string to that value is known.
The Problem
Imagine you have a JSON file that looks something like this:
[[See Video to Reveal this Text or Code Snippet]]
You have the path to the value you want to change stored as a string in a variable called $name. For example:
[[See Video to Reveal this Text or Code Snippet]]
You want to update the loginName under primaryContact to a new value, but you're unsure how to dynamically reference that nested structure.
Attempting to execute the following command:
[[See Video to Reveal this Text or Code Snippet]]
will lead to an error:
[[See Video to Reveal this Text or Code Snippet]]
Understanding the Issue
The error arises because PowerShell tries to resolve $name as a single property, leading it to search for a property named primaryContact.loginName. Instead, you need to navigate down through each level of the object individually to set the desired value.
The Solution: Using the Set-ByPath Function
To successfully update a nested JSON property with a dynamic path, you'll need to create a custom function—let's call it Set-ByPath. This function will take three parameters:
$RootObject: The root JSON object you're working with.
$Path: The path string to the specific value you want to change.
$NewValue: The new value you want to set.
Step-by-Step Breakdown
Here’s how to implement the Set-ByPath function:
[[See Video to Reveal this Text or Code Snippet]]
Using the Function
With the Set-ByPath function defined, you can easily update your JSON data. Here’s how you can use it:
[[See Video to Reveal this Text or Code Snippet]]
This code performs the following:
Defines a Path: The string variable $name contains the path to the value you want to update.
Loads JSON Data: Converts a JSON string into a PowerShell object.
Calls the Function: Uses the Set-ByPath function to update the loginName field.
Conclusion
With the Set-ByPath function, you can dynamically update values in nested JSON structures by referencing paths in a simple string format. This not only simplifies your scripting tasks but also enhances the overall flexibility of your scripts.
Next time you need to update a JSON file in PowerShell, use this method, and make your coding life much easier!

Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: