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
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"}
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)
I have the below JSON which is just a bit of the file but illustrates it.
I am not that experienced with JSON and I have tried a lot of the examples that I could find but they only seem to show JSON that is just key/value pairs. I've seen a few to target arrays but I cant translate them to my particular JSON
for example I'm trying to get that the 'SavedPlayerDataVersion' is equal to 8 into a variable and I've put it into an object but I cant figure out how to target that particular item within the array and object
could anyone point me in the right direction please?
Thank you
{
"profileVersion": 1,
"profile": {
"id": 0,
"class": "PrimalPlayerDataBP_C",
"names": ["PrimalPlayerDataBP_C_29", "ArkGameMode", "PersistentLevel", "Aberration_P", "/Game/Maps/Aberration/Aberration_P"],
"properties": [{
"name": "SavedPlayerDataVersion",
"type": "IntProperty",
"value": 8
}, {
"name": "HexagonCount",
"type": "IntProperty",
"value": 106860
}, {
"name": "MyPersistentBuffDatas",
"type": "ArrayProperty",
"arrayType": "ObjectProperty",
"value": [1, 2, 3, 4]
}],
"extra": null
}
}
"type": "IntProperty",
"value": 8
}, {
"name": "HexagonCount",
"type": "IntProperty",
"value": 6800
}, {
"name": "NumChibiLevelUpsData",
"type": "IntProperty",
"value": 2
}, {
"name": "MyData",
"type": "StructProperty",
"structType": "PrimalPlayerDataStruct",
"value": [{
"name": "PlayerDataID",
"type": "UInt64Property",
"value": 656195017
}, {
"name": "UniqueID",
"type": "StructProperty",
"structType": "UniqueNetIdRepl",
"value": {
"unk": 8,
"netId": "76561198046328344"
}
}
I have edited the question after the original answers as I realised that what I also needed to access was a 'level' deeper. so it would be getting at "PlayerDataID"
I tried $value = ($jsonobj.profile.properties.mydata | Where-Object { $_.name -eq "PlayerDataID" }).value but that isnt getting it for me
JSON is exactly key-value pairs, so handling it like an object in PowerShell is the way to go. Start with ConvertFrom-Json and put the result in a variable, let's say $myJson.
$myJson.profile.class would net you PrimalPlayerDataBP_C. That's how you access nested properties. As for the arrays, you can iterate through them with foreach, get specific values with where or select, depends on what you need actually.
# This one returns the version number
$version = ($myJson.profile.properties | Where {$_.name -eq "SavedPlayerDataVersion"}).value
# This one checks whether version is 8
$isVersion8 = $NULL -ne `
($myJson.profile.properties | Where {$_.name -eq "SavedPlayerDataVersion" -and $_.value -eq 8})
Be careful when accessing array items using their index, as you never can be sure that the item with a specific index is the one you're looking for.
As I'm writing this answer, #Theo beat me to it, so kudos to him for the one-liner.
EDIT:
Including a step-by-step example for clarity's sake. You can do the same with fewer lines.
# Get an array of properties
$properties = $myJson.profile.properties
# Select the property with the required name from the array
$myData = $properties | Where {$_.name -eq "MyData"}
# MyData is multivalued, so need to use 'Where' again
$playerDataId = $myData.value | Where {$_.name -eq "PlayerDataID"}
# Value you are looking for
$playerDataId.value
You can get the value you seek with a one-liner.
For demo I'm using a Here-String for your json data, but probably you would read that from file with $data = Get-Content -Path 'thefile.json' -Raw | ConvertFrom-Json
$data = #'
{
"profileVersion": 1,
"profile": {
"id": 0,
"class": "PrimalPlayerDataBP_C",
"names": ["PrimalPlayerDataBP_C_29", "ArkGameMode", "PersistentLevel", "Aberration_P", "/Game/Maps/Aberration/Aberration_P"],
"properties": [{
"name": "SavedPlayerDataVersion",
"type": "IntProperty",
"value": 8
}, {
"name": "HexagonCount",
"type": "IntProperty",
"value": 106860
}, {
"name": "MyPersistentBuffDatas",
"type": "ArrayProperty",
"arrayType": "ObjectProperty",
"value": [1, 2, 3, 4]
}],
"extra": null
}
}
'# | ConvertFrom-Json
Then, to get the value from property with name "SavedPlayerDataVersion" you simply do:
$value = ($data.profile.properties | Where-Object { $_.name -eq "SavedPlayerDataVersion" }).value
Variable $value will now contain 8
Simple example:
$json = '{ "one": 1, "two": 2 }'
$data = $json | ConvertFrom-Json | Select one
$myVal = $data.one
$myVal
The printed output will be 1
In your case:
$json = '{
"profileVersion": 1,
"profile": {
"id": 0,
"class": "PrimalPlayerDataBP_C",
"names": ["PrimalPlayerDataBP_C_29", "ArkGameMode", "PersistentLevel", "Aberration_P", "/Game/Maps/Aberration/Aberration_P"],
"properties": [{
"name": "SavedPlayerDataVersion",
"type": "IntProperty",
"value": 8
}, {
"name": "HexagonCount",
"type": "IntProperty",
"value": 106860
}, {
"name": "MyPersistentBuffDatas",
"type": "ArrayProperty",
"arrayType": "ObjectProperty",
"value": [1, 2, 3, 4]
}],
"extra": null
}}'
$data = $json | ConvertFrom-Json | Select -expand profile | Select -expand properties | Select name, type, value
$myVal = $data[0].value
$myVal
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.