janhoedt
asked on
Mass edit json Powershell
Hi,
I have lots of jsons which I d need to mass edit (add entries, remove, edit, based upon some logic). F.e. if entry entry1.date.request is not existing add with value y etc
I d like to to this Powershell based. Also json validation needs to occur (syntax ok).
Maybe there is a tool also as backup/extra checks.
J
I have lots of jsons which I d need to mass edit (add entries, remove, edit, based upon some logic). F.e. if entry entry1.date.request is not existing add with value y etc
I d like to to this Powershell based. Also json validation needs to occur (syntax ok).
Maybe there is a tool also as backup/extra checks.
J
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
why the -dept 24?
By default, the Depth is 2 which means if you have multiple complex objects as properties you only go 2 levels down as JSON formatted properties. 24 is going to cover most things, but you can alter this if you don't need to record that many layers of object properties. Here's some sample code:
$object1 = New-Object -TypeName PSObject
$object1 | Add-Member -Type NoteProperty -Name "name" -Value "obj1"
$object2 = New-Object PSObject
$object2 | Add-Member -Type NoteProperty -Name "logdate" -Value Get-Date
$object2 | Add-Member -Type NoteProperty -Name "status" -Value "success"
$object3 = New-Object PSObject
$object3 | Add-Member -Type NoteProperty -Name "file" -Value "E:\something\file.text"
$object4 = New-Object PSObject
$object4 | Add-Member -Type NoteProperty -Name "isBackedUp" -Value $true
$object4 | Add-Member -Type NoteProperty -Name "size" -Value "1024 kb"
$object3 | Add-Member -Type NoteProperty -Name "detail" -Value $object4
$object2 | Add-Member -Type NoteProperty -Name "file1" -Value $object3
$object2 | Add-Member -Type NoteProperty -Name "file2" -Value $object3
$object2 | Add-Member -Type NoteProperty -Name "file3" -Value $object3
$object1 | Add-Member -Type NoteProperty -Name "logs" -Value $object2
$object1 | ConvertTo-Json -Depth 24 | Set-Content "E:\object1.json"
$object1 | ConvertTo-Json | Set-Content "E:\object1_default.json"
Open the 2 files and you can see the difference.
what if you want to check if a certain parameter exists, if not create it?You would use Add-Member
$record.details | Add-Member -Type NoteProperty -Name "detail_3" -Value "something_else_entirely"
ASKER
-why the -dept 24?
-what if you want to check if a certain parameter exists, if not create it?
f.e.
"newsection": 'newvalue'
"details": {
"detail_3": "somethingelse",