-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoreplace-value-in-nested-json.ps1
57 lines (49 loc) · 1.86 KB
/
autoreplace-value-in-nested-json.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Ensure the necessary assembly is loaded
Add-Type -AssemblyName System.Windows.Forms
function Get-FolderName {
# Create an OpenFileDialog object
$dialog = New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = [Environment]::GetFolderPath('Desktop')
CheckFileExists = $false
ValidateNames = $false
FileName = "Select Folder"
}
# Show the dialog
$result = $dialog.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
# Get the folder part of the selected path
return ([System.IO.Path]::GetDirectoryName($dialog.FileName))
} else {
return $null
}
}
$folder = Get-FolderName
if ($folder) {
Get-ChildItem -Path $folder -Filter *.json -Recurse -File | ForEach-Object {
$filename = $_.FullName
$a = Get-Content $filename -raw | ConvertFrom-Json
$a.inventurBezeichnung = 11
if ($a.bestandsErfassung -and $a.bestandsErfassung -is [System.Array]) {
foreach($item in $a.bestandsErfassung) {
if ($item.PSObject.Properties.Name -contains 'erfasserKurzzeichen') {
$item.erfasserKurzzeichen = 5
}
if ($item.zaehlSaetze -and $item.zaehlSaetze -is [System.Array]) {
foreach ($product in $item.zaehlSaetze) {
if ($product.PSObject.Properties.Name -contains 'lagerplatzBezeichnung') {
$product.lagerplatzBezeichnung = $product.lagerplatzBezeichnung -replace '-','/'
}
if ($product.PSObject.Properties.Name -contains 'artikelNr') {
if (-not $product.artikelNr.EndsWith('..')) {
$product.artikelNr += '..'
}
}
}
}
}
}
$a | ConvertTo-Json -Depth 100 | Out-File $filename -Encoding UTF8
Write-Host "File $($filename).json was changed."
}
}
else { "You did not select a directory." }