How to get particular values from JSON using powershell - json

I have to get the values from JSON file for my test report purpose.
Can anyone help me how get values of passes, failure from the below JSON data.
{
"stats": {
"suites": 2,
"tests": 4,
"passes": 3,
"pending": 0,
"failures": 0,
"start": "2021-11-05T15:58:26.817Z",
"end": "2021-11-05T15:59:26.701Z",
"duration": 55162,
"testsRegistered": 4,
"passPercent": 75,
"pendingPercent": 0,
"other": 0,
"hasOther": false,
"skipped": 1,
"hasSkipped": true
},
}
I have tried below code, but not getting correct result
$json= Get-Content -Raw -Path 'C:\report.json' | ConvertFrom-Json
write-host "passes : $($json.passes)"
write-host "failures : $($json.failures)"
Result:
$json= Get-Content -Raw -Path 'C:\report.json' | ConvertFrom-Json
write-host "passes : $($json.passes)"
write-host "failures : $($json.failures)"
passes :
failures :
Edit:
I have got the solution by adding below code
$json= Get-Content -Raw -Path 'C:\report.json' | ConvertFrom-Json
write-host "passes : $($json.stats.passes)"
write-host "failures : $($json.stats.failures)"

try this
$json= Get-Content -Raw -Path <jsonFile>.json | ConvertFrom-Json
//or
$json = $response | ConvertFrom-Json //if you have it from url
write-host "passes : $($json.passes)"
write-host "failures : $($json.failures)"

I have got the solution by adding below code
$json= Get-Content -Raw -Path 'C:\report.json' | ConvertFrom-Json
write-host "passes : $($json.stats.passes)"
write-host "failures : $($json.stats.failures)"
Result:
passes : 14
failures : 0

Related

Powershell - reading json file - how to update a value for a field name that isn't unique

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

(Powershell) Read and change JSON value to a date

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": {

Ignoring files in Get-ChildItem from objects being loaded from JSON

I have a script that I am using to read a JSON file, read the paths, calculate the MD5 hash and then export them in a CSV file. The JSON file contains multiple objects. In another post, I laid out the structure of the JSON file and I'll post that here. Please note that I am not allowed to change this JSON.
{
"destinationpath": "C:\\Destination\\Mobile Phones\\"
"sourcepath": "C:\\Source\\Mobile Phones\\"
"OnePlus" : {
"files": [
{
"source": "6T",
"destination": "Model\\6T",
"log": "logfiles",
"version": "Version.txt",
}
]
}
"Samsung" : {
"files": [
{
"source": "S20",
"destination": "Galaxy\\S20",
"log": "logfiles",
"version": "Version.txt",
}
]
}
}
The destinationpath: C:\\Destination\\Mobile Phones\\, when added to, for example, Samsung it becomes: destinationpath: C:\\Destination\\Mobile Phones\\Galaxy\\S20.
The script that I have so far is this:
$JSON = Get-Content -Path file.json | ConvertFrom-Json
$JSONdestination = $JSON.destination
$JSONsource = $JSON.source
foreach ($i in $JSON.psobject.properties) {
foreach($i2 in $i.Value ){
$IgnoredFiles = #(
$logfiles = $i2.files.log
$newFiles = $i2.files.version
)
$destination = $JSON + $i2.files.destination
$source = $JSONdestination + $i2.files.source
echo $destination
Get-ChildItem -Path $destination -Recurse -Exclude $IgnoredFiles | Get-FileHash -Algorithm MD5 -ErrorAction SilentlyContinue | Export-csv -Append -Path "C:\Users\home\Downloads\hashes.csv"
}
In the Get-ChildItem portion, I want to exclude the "log" and "version" files so that they are not counted in the Get-FileHash. I have tried doing it this way as well:
Get-ChildItem -Path $destination -Recurse -Exclude $i2.files.log, $i2.files.version | Get-FileHash -Algorithm MD5 -ErrorAction SilentlyContinue | Export-csv -Append -Path "C:\Users\home\Downloads\hashes.csv"
However, it doesn't work either and the log and version files are still taken into account in the exported csv.
The json you show us is invalid (missing commas and commas too many..)
Once fixed into
{
"destinationpath": "C:\\Destination\\Mobile Phones\\",
"sourcepath": "C:\\Source\\Mobile Phones\\",
"OnePlus": {
"files": [{
"source": "6T",
"destination": "Model\\6T",
"log": "*.log",
"version": "Version.txt"
}]
},
"Samsung": {
"files": [{
"source": "S20",
"destination": "Galaxy\\S20",
"log": "*.log",
"version": "Version.txt"
}]
}
}
you can do this:
$JSON = Get-Content -Path file.json | ConvertFrom-Json
$JSONdestination = $JSON.destinationpath
$JSONsource = $JSON.sourcepath
$result = foreach ($item in ($JSON | Select-Object * -ExcludeProperty destinationpath, sourcepath)) {
$item.PSObject.Properties.Value | ForEach-Object {
$destPath = Join-Path -Path $JSONdestination -ChildPath $_.files.destination
$logFolder = Join-Path -Path $JSONdestination -ChildPath $_.files.log
$exclude = $_.files.version
Write-Host "Processing files in '$destPath'" -ForegroundColor Cyan
Get-ChildItem -Path $destPath -Recurse -Exclude $exclude -ErrorAction SilentlyContinue |
Where-Object {$_.FullName -notlike "$logFolder*" } |
Get-FileHash -Algorithm MD5
}
}
$result | Export-Csv -Path "C:\Users\home\Downloads\hashes.csv" -NoTypeInformation

PowerShell Loop through nested json and remove property

I am getting some json data from a API, however I don't need most of this data. I am trying to remove some fields so that the when I save this data as a json file it isn't so large. I doesnt seem to be removing any of the fields I am trying to remove.
Code:
$Response = Invoke-RestMethod -Uri "https://mtgjson.com/api/v5/AllPrintings.json" -Method GET
$Obj = ConvertFrom-Json $Response
$Obj.PSObject.Properties.Remove('booster')
$Obj.PSObject.Properties.Remove('cards')
$Obj | ConvertTo-Json | Out-File ./All-Sets-Data.json -Force
Json:
{
"data": {
"10E": {
"baseSetSize": 383,
"block": "Core Set",
"booster": "#{default=}",
"cards": "",
"code": "10E",
...
},
"2ED": {
"baseSetSize": 302,
"block": "Core Set",
"booster": "#{default=}",
"cards": "",
"code": "2ED",
...
},
"2XM": {
"baseSetSize": 332,
"booster": "#{default=}",
"cards": "",
"code": "2XM",
...
},
...
}
}
$Obj.data.'10E'.PSObject.Properties.Remove('booster')
$Obj.data.'10E'.PSObject.Properties.Remove('cards')
$Obj.data.'2ED'.PSObject.Properties.Remove('booster')
# and so on
The above code snippet should work. However, you can do all in a one step by calling the following (recursive) function RemoveProperty:
Function RemoveProperty {
param (
# A PSCustomObject
[Parameter( Mandatory, ValueFromPipeline )] $Object,
# A list of property names to remove
[Parameter( Mandatory )] [string[]]$PropList,
# recurse?
[Parameter()] [Switch]$Recurse
)
# Write-Host $Object -ForegroundColor Cyan
foreach ( $Prop in $PropList ) {
$Object.PSObject.Properties.Remove($prop)
}
# Write-Host $Object -ForegroundColor Green
if ( $Recurse.IsPresent ) {
foreach ($ObjValue in $Object.PSObject.Properties.Value) {
# Write-Host $ObjValue -ForegroundColor Yellow
if ( $ObjValue.GetType().Name -eq 'PSCustomObject' ) {
$ObjValue | RemoveProperty -PropList $PropList -Recurse
}
}
}
}
# sample usage:
$Obj = ConvertFrom-Json $Response
RemoveProperty -Object $Obj -PropList 'booster','cards' -Recurse
$Obj | ConvertTo-Json | Out-File ./All-Sets-Data.json -Force
(Please note that the RemoveProperty function contains some Write-Host in commented lines; originally used used for debugging purposes).

Unable to copy json block

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'