Home > Net >  Filtering Json data, no results
Filtering Json data, no results

Time:09-22

MAIL SNIPPETMy JSON file export.json has this content.

[
    {
        "Workspace Name":  "IAP IPP DW - PRD",
        "Workspace Allowance":  "130",
        "Workspace Usage":  "108.9413",
        "Workspace Size Free":  "21.1",
        "Percentage Utilization":  "0.83801",
        "Predicted Usage":  "146.9888958"
    },
      {
        "Workspace Name":  "Connected planning Facilities-PRD",
        "Workspace Allowance":  "130",
        "Workspace Usage":  "81.2462",
        "Workspace Size Free":  "48.8",
        "Percentage Utilization":  "0.624970769",
        "Predicted Usage":  "85.79967522"
      },
      {
        "Workspace Name":  "Lubes SDA - PROD",
        "Workspace Allowance":  "100",
        "Workspace Usage":  "78.5773",
        "Workspace Size Free":  "21.4",
        "Percentage Utilization":  "0.785773",
        "Predicted Usage":  "87.03502547"
      },
        {
        "Workspace Name":  "IAP IPP DW - ACC",
        "Workspace Allowance":  "100",
        "Workspace Usage":  "56.272",
        "Workspace Size Free":  "43.7",
        "Percentage Utilization":  "0.56272",
        "Predicted Usage":  "95.58173647"
    },
    {
        "Workspace Name":  "ONE Forecast UPD - PRD",
        "Workspace Allowance":  "100",
        "Workspace Usage":  "42.1855",
        "Workspace Size Free":  "57.8",
        "Percentage Utilization":  "0.421855",
        "Predicted Usage":  "51.76880067"
    },
    {
        "Workspace Name":  "IAP IPP DW - DEV",
        "Workspace Allowance":  "50",
        "Workspace Usage":  "38.7354",
        "Workspace Size Free":  "11.3",
        "Percentage Utilization":  "0.774708",
        "Predicted Usage":  "33.98943696"
    }
]

When I write the below code in POWER SHELL it displays the data set for "Connected planning Facilities-PRD" and "Lubes SDA - PROD" but does not display for "IAP IPP DW - PRD" which should ideally show up because the Workspace Usage for that Model is also 108.

$variable =Get-Content "export.json" | ConvertFrom-Json
#Write-Output $variable
$logs=$variable | Where-Object { $_.'Workspace Usage' -ge "75" } 
Write-Output $logs

PLEASE HELP

CodePudding user response:

$variable =Get-Content -raw "export.json" | ConvertFrom-Json $logs=$variable | Where-Object { $_.'Workspace Usage' -gt 75 }

Write-Output $logs

Output

Workspace Name : Connected planning Facilities-PRD Workspace Allowance : 130 Workspace Usage : 81.2462 Workspace Size Free : 48.8 Percentage Utilization : 0.624970769 Predicted Usage : 85.79967522

Workspace Name : Lubes SDA - PROD Workspace Allowance : 100 Workspace Usage : 78.5773 Workspace Size Free : 21.4 Percentage Utilization : 0.785773 Predicted Usage : 87.03502547

CodePudding user response:

The issue is all of the JSON deserialized values are strings. You are intending to do a numerical comparison and must ensure all data being compared are numeric types.

$variable | Where { [double]$_.'Workspace Usage' -ge 75 }

Output:

Workspace Name         : IAP IPP DW - PRD
Workspace Allowance    : 130
Workspace Usage        : 108.9413
Workspace Size Free    : 21.1
Percentage Utilization : 0.83801
Predicted Usage        : 146.9888958

Workspace Name         : Connected planning Facilities-PRD
Workspace Allowance    : 130
Workspace Usage        : 81.2462
Workspace Size Free    : 48.8
Percentage Utilization : 0.624970769
Predicted Usage        : 85.79967522

Workspace Name         : Lubes SDA - PROD
Workspace Allowance    : 100
Workspace Usage        : 78.5773
Workspace Size Free    : 21.4
Percentage Utilization : 0.785773
Predicted Usage        : 87.03502547

The left-hand side (LHS) of an operator influences the type of the RHS of the operator if a type conversion can be done on the RHS. Thus, by casting the LHS to [double] the number on the RHS (75) is converted to [double] also. Then the comparison executes as expected.

  • Related