How to copy latest Json data from another Json file PowerShell? - json

I have a source.Json file like below
{
"kind": "sql#backupRunsList",
"items": [
{
"kind": "sql#backupRun",
"status": "SUCCESSFUL",
"enqueuedTime": "2023-01-11T00:33:21.903Z",
"id": "1673391600000",
"startTime": "2023-01-11T00:33:21.949Z",
"endTime": "2023-01-11T00:38:47.459Z",
"type": "AUTOMATED",
"windowStartTime": "2023-01-10T23:00:00Z",
"instance": "instance",
"selfLink": "https://sqladmin.googleapis.com/v1/projects/project/instances/instance/backupRuns/1673391600000",
"location": "us",
"backupKind": "SNAPSHOT"
},
{
"kind": "sql#backupRun",
"status": "SUCCESSFUL",
"enqueuedTime": "2023-01-09T23:36:39.776Z",
"id": "1673305200000",
"startTime": "2023-01-09T23:36:39.826Z",
"endTime": "2023-01-09T23:42:05.542Z",
"type": "AUTOMATED",
"windowStartTime": "2023-01-09T23:00:00Z",
"instance": "instance ",
"selfLink": "https://sqladmin.googleapis.com/v1/projects/project/instances/instance/backupRuns/1673305200000",
"location": "us",
"backupKind": "SNAPSHOT"
},
And I have a target.json file below
{
"restoreBackupContext":
{
"backupRunId": 123456,
"project": "project",
"instanceId": "instance"
}
}
How to copy the data "id": "1673391600000", from source.json to "backupRunId": value to target.json
In target backupRunId value "123456" should be replace to "1673391600000" value using power shell.
getting id value from source file, How to copy to target file
$content=Get-Content -Path source.json
$JsonData=$content | ConvertFrom-Json
$JsonData.Items.id[0]
Please any one suggest

If you only want to process the first item in $JsonData.Items and update the single target json file
# Grab json text from source file and convert to PSobjects
$source = Get-Content .\source.json | ConvertFrom-Json
# Grab json text from target file and convert to PSobjects
$target = Get-Content .\target.json | ConvertFrom-Json
# Update $target object with id from $source object
$target.restoreBackupContext.backupRunId = $source.items[0].id
# Take updated $target object, convert back to json and save back to target.json file
$target | ConvertTo-Json | Set-Content -Path .\target.json

Related

Extract Value from JSON Using PowerShell based on condition

I have the captured the JSON response in variable $releaseDefinitions. With this i want to extract the "ID" as "4598" when i pass the "name" as "STAGE1-PG-DB" under the "environments" tag using the powershell.
Any help on this is much appreciated.
{
"id": 516,
"environments": [
{
"id": 4598,
"releaseId": 516,
"name": "STAGE1-PG-DB",
"status": "notStarted",
},
{
"id": 4599,
"releaseId": 516,
"name": "STAGE2-PG-DB",
"status": "notStarted",
},
{
"id": 4600,
"releaseId": 516,
"name": "STAGE3-PG-DB",
"status": "notStarted",
}
]
}
I believe you are asking to get the JSON array object where the name is "STAGE1-PG-DB".
Based on the info you've provided, you would do something like this (see my in-line comments)
$releaseDefinitions = Get-Content -Path $inputFileName -Raw | ConvertFrom-Json
# use dot-notation to get the entire Environments array
# pipe the array through using the pipe character
# filter through the array where the key (Name) is equal to your value (STAGE1-PG-DB)
$releaseDefinitions.environments | Where-Object {$_.name -eq "STAGE1-PG-DB"}

How to read specific Key from JSON file with Powershell

I'm trying to read a Json file with some passwords but them are stored into Key-Value pattern as below:
{
"id": "a8cc4184-3844-4605-8e35-ea479ae1094e",
"name": "Test Credentials and Secrets",
"values": [
{
"key": "apassword",
"value": "mypassword",
"enabled": true
},
{
"key": "tpassword",
"value": "my other password",
"enabled": true
},
],
"exported_using": "Postman/7.34.0"
}
How can I get any specific value with PowerShell? tpassword value for instance.
I would recommend using ConvertFrom-Json to turn it into an Object so you can then access the properties easily with PowerShell.
I saved the json above to C:\temp\json.json and then the following got me the tpassword value:
$json = Get-Content C:\temp\json.json | ConvertFrom-Json
$json.values | Where-Object key -eq 'tpassword' | Select-Object Value

powershell that reads values from csv or json file and create registry keys if do not exist

I am trying to write a powershell script that creates registry keys and their values from a csv or json file containing the list of the registries .
did any one code such thing can help ? :)
Using ConvertFrom-Json, convert your JSON to a custom PSCustomObject object. Then iterate through the object properties and call New-ItemProperty to create the new registry entries with the appropriate values.
PowerShell
$jsonFile = "<path to JSON file>"
$customObject = Get-Content $jsonFile | ConvertFrom-Json
$customObject.PSObject.Properties | ForEach-Object {
[void](New-ItemProperty -LiteralPath $_.Value.path -Name $_.Value.name -Value $_.Value.value -PropertyType $_.Value.type -Force)
}
JSON
{
"reg1": {
"path": "path1",
"name": "name1",
"value": "value1",
"type": "type1"
},
"reg2": {
"path": "path2",
"name": "name2",
"value": "value2",
"type": "type2"
},
"reg3": {
"path": "path3",
"name": "name3",
"value": "value3",
"type": "type3"
}
}
Links
ConvertFrom-Json (learn.microsoft.com)
New-ItemProperty (learn.microsoft.com)

How to select words after /slash from a json file in a powershell script

I've a json file from a az cli command, I want to deploy with a powershell script from a tool like jenkins or rundeck, I need to take the field "name" from json but not entirely only the word after forward slash
[
{
"Group": "KC-EMEA-RSGP-BPTRAINING-01",
"id": "/subscriptions/KC-EMEA-RSGP-BPTRAINING-01/providers/Microsoft.Sql/servers/",
"name": "kc-emea-sqsrv-bptraining-dev-01/master"
},
{
"Group": "KC-EMEA-RSGP-NAVISIONKM-DEV-01",
"id": "/subscriptions/KC-EMEA-RSGP-NAVISIONKM-DEV-01/providers/Microsoft.Sql/servers/",
"name": "km-emea-sqsrv-navision-tst-01/km-emea-sqdb-navision-tst-01"
},
{
"Group": "KC-EMEA-RSGP-NAVISIONKM-DEV-01",
"id": "/subscriptions/KC-EMEA-RSGP-NAVISIONKM-DEV-01/providers/Microsoft.Sql/servers/",
"name": "km-emea-sqsrv-navision-tst-01/master"
},
{
"Group": "KC-EMEA-RSGP-PROJECTS-DEV-01",
"id": "/subscriptions/KC-EMEA-RSGP-PROJECTS-DEV-01/providers/Microsoft.Sql/servers/",
"name": "kc-emea-sqsrv-projects-dev-01/KC-EMEA-SQDB-BPTRAINING-TRAINEE-01"
}
]
$file = "pathtojsonfile.json"
$jsonContent = Get-Content $file | ConvertFrom-Json;
$namedb = $jsonContent.name;
$value= $namedb.Substring($namedb.IndexOf('/')+1) --> this doesn't work.
Try
$namedb[0].Substring($namedb[0].IndexOf('/')+1)
And for all of them:
foreach($name in $namedb) {$name.Substring($name.IndexOf('/')+1)}

ConvertFrom-JSON won't accept convertto-json with children when working with WebServiceProxy

I am pulling data from an API using the New-WebServiceProxy in PowerShell 4.0 and then piping it out to a JSON file for review and import on another API service (same API version, etc, just a different host).
$tasklist.Taskconfig | ConvertTo-JSON-Depth 50 -As String | Out-File -FilePath $exportpath\$name.xml -Force
Gives me my XML containing the TaskConfig. In this case, TaskConfig is an object type automatically generated by the API I'm interfacing with. When I want to import the content I am using:
$taskconfig = (Get-Content "$taskjson") -join "`n" | ConvertFrom-Json
but when I run this it's unable to create the object. I assume this is because the JSON contains nested children, giving the error-
Cannot convert value "#{Name=plugindive; Value=;> Children=System.Object[]}" to type "Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1rcleWeb_WebClientAPI_asmx_wsdl.TaskConfig". Error: "Cannot convert the "#{Name=plugindive; Value=;Children=System.Object[]}" value of type "System.Management.Automation.PSCustomObject" to type "Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1rcleWeb_WebClientAPI_asmx_wsdl.TaskConfig"."
I've tried explictly stating the type of object:
$taskconfig = [Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1rcleWeb_WebClientAPI_asmx_wsdl.TaskConfig](Get-Content "$taskjson" | Out-string | ConvertFrom-Json)
as well as creating the object then trying to add the children from my JSON -
$taskconfig.children = $json.children
But these all fail in the same way.
I don't seem to get this same issue in PowerShell 5.0 interestingly enough, but I can't verify why - is there another way to approach this?
Added example JSON below
{"Name": "plugindive",
"Value": null,
"Children": [{
"Name": "auto",
"Value": "False",
"Children": [
]
},
{
"Name": "categories",
"Value": null,
"Children": [{
"Name": "Module Z",
"Value": "False",
"Children": [
]
},
{
"Name": "Module A",
"Value": "False",
"Children": [
]
},
{
"Name": "Module B",
"Value": "False",
"Children": [
]
},
{
"Name": "Module C",
"Value": "False",
"Children": [
]
}
]
}
]
}
It seems as if this doesn't work in PowerShell v3.0, so I simply ended up making posts with the explicit XML directly, rather than converting to JSON.