I'm trying to copy condition block from id 106 to id 107 with script below; however, I got error "ConvertFrom-Json : Invalid JSON primitive:". Am I missing something? Please advise.
$a = Get-Content 'C:\DevOps\mytest.json' -raw | ConvertFrom-Json
$condition = $a | select -expand environments | select -expand conditions
Write-Host $condition[0]
$a | add-member -Name $condition[1].name -value (Convertfrom-Json $condition[0]) -MemberType NoteProperty
Write-Host $update
$a | ConvertTo-Json -Depth 20 | set content 'C:\DevOps\updatedmytest.json'
The JSON file contains:
{
"source": "userInterface",
"id": 25,
"revision": 3,
"environments": [
{
"id": 106,
"conditions": [
{
"name": "ReleaseStarted",
"conditionType": "event",
"value": ""
}
]
},
{
"id": 107,
"conditions": [
]
}
]
}
Best Regards,
Andy Pham
The error message you are receiving is because you convert the JSON into a PowerShell Object and then saved it into the variable $a. $condition then is still a PowerShell object that you have expanded and is not JSON. So -value (Convertfrom-Json $condition[0]) doesn't work because it's already converted.
Convert to an object, make changes directly to the object, convert from the object.
$ConvertedJSON = Get-Content 'C:\DevOps\mytest.json' -Raw | ConvertFrom-Json
$ConvertedJSON.environments[1].conditions = $ConvertedJSON.environments[0].conditions
$ConvertedJSON | ConvertTo-Json -Depth 20 | Set-Content 'C:\DevOps\updatedmytest.json'
Alternatively, you could use Where-Object to filter for the ids.
$ConvertedJSON = Get-Content 'C:\DevOps\mytest.json' -Raw | ConvertFrom-Json
($ConvertedJSON.environments | Where-Object {$_.id -eq 107}).conditions = ($ConvertedJSON.environments | Where-Object {$_.id -eq 106}).conditions
$ConvertedJSON | ConvertTo-Json -Depth 20 | Set-Content 'C:\DevOps\updatedmytest.json'
Related
This question already has answers here:
Unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2
(2 answers)
Closed 11 months ago.
The owners key in my output (see OutputFile) I'm expecting as a line separated array, but it's outputting as a single-line space separated object/string
Script:
function Add-ApplicationOwner
{
param (
[string] $App,
[object] $OutputObject
)
# add values to our json output
$owners = (Get-AzureAdApplicationOwner -ObjectId $App).UserPrincipalName
$OutputObject | Add-Member -MemberType NoteProperty -Name owners -Value $owners
}
$inputFile = Get-Content -Path "AppInput.json" -Raw | ConvertFrom-Json
$outputFile = New-Object -TypeName PsObject
foreach ($object in $inputFile.PSObject.Properties)
{
$outputAppList = New-Object -TypeName PsObject
foreach ($app in $inputFile.New.PsObject.Properties)
{
# create app
$appRegistration = New-AzureADApplication -DisplayName "TestSPN1"
#add application info into json object
$outputAppValues = [PsCustomObject]#{
app_id = $appRegistration.AppId
}
#add application owners by object id
Add-ApplicationOwner -App $appRegistrationObjectId -OutputObject $outputAppValues
$outputAppList | Add-Member -MemberType NoteProperty -Name "TestSPN1" -Value $outputAppValues
}
# add all created apps into json output file
$outputFile | Add-Member -MemberType NoteProperty -Name "New Applications" -Value $outputAppList
}
$outputFile | ConvertTo-Json | Out-File "AzADAppRegistrationInfo.json" -Append
OutputFile:
{
"New Applications": {
"TestSPN1": {
"app_id": "dsfadfdafa-3afadfdafadsfasd-343",
"owners": "user1 user2 user3"
}
}
}
Desired Output:
{
"New Applications": {
"TestSPN1": {
"app_id": "dsfadfdafa-3afadfdafadsfasd-343",
"owners": [
"user1",
"user2",
"user3"
]
}
}
}
$owners Variable Examined:
$owners
user1
user2
user3
$owners.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
When I look at $outputFile.'New Applications' it's as expected
$outputFile.'New Applications' | convertto-json
{
"TestSPN1": {
"app_id": "asdfdsfad",
"owners": [
"user1",
"user2",
"user3"
]
}
}
When I look at $outputFile it's flattened
$outputFile | convertto-json
{
"New Applications": {
"TestSPN1": {
"app_id": "cc6dgfsdgdsgfsdgfdsa5562614",
"owners": "user1 user2 user3"
}
}
}
The most likable explanation for your issue is that -Depth is using the default Value. I have stored $outputFile.'New Applications' in the $json variable for below example.
[pscustomobject]#{
'New Applications' = $json
} | ConvertTo-Json
Results in:
WARNING: Resulting JSON is truncated as serialization has exceeded the set depth of 2.
{
"New Applications": {
"TestSPN1": {
"app_id": "asdfdsfad",
"owners": "user1 user2 user3"
}
}
}
Worth pointing out that the Warning Message is only displayed on newer versions of PowerShell (PS 7.1+ to be precise, thanks mklement0 for pointing it out). Windows PowerShell defaults to truncate the JSON without any warning.
However if we add 1 depth level (Default -Depth value is 2):
[pscustomobject]#{
'New Applications' = $json
} | ConvertTo-Json -Depth 3
Results in:
{
"New Applications": {
"TestSPN1": {
"app_id": "asdfdsfad",
"owners": [
"user1",
"user2",
"user3"
]
}
}
}
I have the following JSON:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"field1": {
"value": "fillmein"
},
"field2": {
"value": "fillmein"
},
"field3": {
"value": "fillmein"
}
}
}
I need to be able to look up each field at different points, and fill them in. For example:
"field1": {
"value": "newv-value"
}
I have the following code so far in my powershell script:
$parameters = Get-Content 'parameters.json' -raw | ConvertFrom-Json -Depth 20
Write-Output $parameters.Values
Write-Output $parameters.Values.psobject.Properties
It's not clear to me how to navigate the object. Both write-output statements return nothing.
I've also tried to use the PSCustomObject, like this:
$parameters = [PSCustomObject] # Get-Content 'parameters.json' -raw | ConvertFrom-Json -Depth 20
But I've messed up the syntax it seems cuz I'm getting an "unrecognized token" error with the "#".
Any tips would be appreciated.
Updating your JSON should be as simple as this:
$json = Get-Content 'parameters.json' -Raw | ConvertFrom-Json
$json.parameters.field1.value = 'hello'
$json.parameters.field2.value = 'world'
$json | ConvertTo-Json
Result would be:
{
"$schema": "https://schema.management.azure.com/schemas/....,
"contentVersion": "1.0.0.0",
"parameters": {
"field1": {
"value": "hello"
},
"field2": {
"value": "world"
},
"field3": {
"value": "fillmein"
}
}
}
As for, how to navigate your object, I guess the key would be to use Get-Member:
PS /> $json | Get-Member -MemberType Properties
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
$schema NoteProperty string $schema=https://schema.management.azure.com/schemas/...
contentVersion NoteProperty string contentVersion=1.0.0.0
parameters NoteProperty System.Management.Automation.PSCustomObject paramete...
PS /> $json.parameters | Get-Member -MemberType Properties
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
field1 NoteProperty System.Management.Automation.PSCustomObject field1=#{value=fillmein}
field2 NoteProperty System.Management.Automation.PSCustomObject field2=#{value=fillmein}
field3 NoteProperty System.Management.Automation.PSCustomObject field3=#{value=fillmein}
PS /> $json.parameters.field1 | Get-Member -MemberType Properties
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
value NoteProperty string value=fillmein
I believe The ".values.psobject" is unnecessary. Try the following:
$myjson= Get-Content 'parameters.json' -raw | ConvertFrom-Json -Depth 20
$parameters.parameters.filed = "new-value"
ConvertTo-Json $myjson -Depth 20 | set-content .\parameters.json
I'm trying to write a powershell script that could read a JSON, its extension is .pros, then change the project name from "test" to "test 12/12 13:24" or something similar. So far I was able to read it, but then I can't get to the project_name field.
my code:
$pros = Get-ChildItem -Path $house -Recurse -Include *.pros
$time = Get-Date -Format 'u'
$pog = Get-Content $pros -raw | ConvertFrom-Json
$pog.update | $_.project_name=$time
$pog | ConvertTo-Json -depth 32| set-content $pros
Write-Output $time
The .pros:
{
"py/object": "pros.conductor.project.Project",
"py/state": {
"project_name": "Marcos_Yuck_new",
"target": "v5",
"templates": {
"kernel": {
"location": "/home/dragon/.config/pros/templates/kernel#3.5.3",
"metadata": {
I want to return only the hash value of one object from my test.json file.
Right now, I am getting all hash values with my code.
json file:
[
{
"name": "abc.txt",
"hash": "D23FC7C4C9F1ED7CD147D7D29E3A541D"
},
{
"name": "def.txt",
"hash": "681B75B81734F7215C2DAD1F7EDFDAF7"
},
{
"name": "ghi.txt",
"hash": "81709CDC04EBDBDAA9BA15F6CAF1F05B"
},
{
"name": "xyz.txt",
"hash": "56F07815D06966FA3A73275797496881"
}
]
My code:
$jsonFile = $PSScriptRoot + "\test.json"
(Get-Content $jsonFile | ConvertFrom-Json | where {$_.name -eq 'abc.txt'}).hash
Careful with the parentheses:
((Get-Content $jsonFile | ConvertFrom-Json) | where {$_.name -eq "abc.txt"}).hash
#D23FC7C4C9F1ED7CD147D7D29E3A541D
Like this your json file contains a list of dictionaries. First you need to select which item of your list you want to have and then you can retrieve the value based on the key.
Try something like this:
$jsonFile = $PSScriptRoot + "\test.json"
$jsonList = Get-Content $jsonFile | ConvertFrom-Json
$jsonList[0].hash
Alternatively:
$jsonObject = $jsonList | Where-Object {$_.name -eq 'abc.txt'} | Select-Object hash
I have one json file mytest.json like below I want to update values using PowerShell script
update.json
{
"update": [
{
"Name": "test1",
"Version": "2.1"
},
{
"Name": "test2",
"Version": "2.1"
}
]
}
I want to write a PowerShell script where if Name=="test1" I want to update Version= "3"
How can i do it using parameters?
Here is a way :
$a = Get-Content 'D:\temp\mytest.json' -raw | ConvertFrom-Json
$a.update | % {if($_.name -eq 'test1'){$_.version=3.0}}
$a | ConvertTo-Json -depth 32| set-content 'D:\temp\mytestBis.json'
According to #FLGMwt and #mikemaccana I improve the ConvertTo-Json with -depth 32 because the default depth value is 2 and for object deeper than 2 you will receive class informations in spite of objects.
I have also faced the same kind of issue. I was looking to change the records of the below JSON file
{
"SQS_QUEUE_URL": "https://que-url.com/server1",
"SQS_EVENTS_QUEUE_URL": "https://events-server.com/server1/development_events",
"REGION": "region1",
"BUCKET": "test-bucket",
"AE_WORK_PATH": "C:\\workpath\\path1",
"ENV": "env"
}
Finally, I managed to find the easiest way to generate a JSON file from Powershell.
$json = Get-Content "c:\users\bharat.gadade\desktop\test.json" | ConvertFrom-Json
$json.SQS_QUEUE_URL = "https://que-url.com/server2"
$json.SQS_EVENTS_QUEUE_URL = "https://events-server.com/Server2/development_events"
$json.REGION = "region1 "
$json.BUCKET = "test-bucket"
$json.AE_WORK_PATH = "C:\workpath\path1"
$json.ENV = "env"
$json | ConvertTo-Json | Out-File "c:\users\bharat.gadade\desktop\test.json"