beta
beta

Reputation: 5686

Filter JSON in Powershell

I have the following JSON in a Powershell variable:

{
  "Object1": {
    "name": "asdf1",
    "criteria": 2
  },
  "Object2": {
    "name": "asdf2",
    "criteria": 1
  }
}

I want to get JSON where the value of criteria is 1. The result therefore should look as follows:

{
  "Object2": {
    "name": "asdf2",
    "criteria": 1
  }
}

I made an attempt with the following code:

$json | Get-ObjectMembers | Select-Object | where { $_.value.criteria -eq 1 };

While this basically goes into the right direction, it is not what I want exactly, because the result looks like this:

{
    "name": "asdf2",
    "criteria": 1
}

See that the Object2 information is lost and one depth-level is lost.

How can I achieve the desired result as shown above?

Upvotes: 1

Views: 3088

Answers (2)

js2010
js2010

Reputation: 27423

The jq solution. with_entries temporarily turns a list of properties into key and value pairs. https://stedolan.github.io/jq/manual/#to_entries,from_entries,with_entries (section id's in html come in handy) (Json creators don't believe in arrays?)

$json = '{
  "Object1": {
    "name": "asdf1",
    "criteria": 2
  },
  "Object2": {
    "name": "asdf2",
    "criteria": 1
  }
}'

$json | jq 'with_entries(select(.value.criteria == 1))'

{
  "Object2": {
    "name": "asdf2",
    "criteria": 1
  }
}

Upvotes: 0

mklement0
mklement0

Reputation: 437608

In essence, you're looking to only retain properties of interest from your single input object or, to put it differently, to remove properties you're not interested in.

Here's a PSv4+ solution:

$json = @'
{
  "Object1": {
    "name": "asdf1",
    "criteria": 2
  },
  "Object2": {
    "name": "asdf2",
    "criteria": 1
  }
}
'@

($json | ConvertFrom-Json).psobject.Properties.
  Where({ $_.Value.criteria -eq 1 }).
    ForEach({ [pscustomobject] @{ $_.Name = $_.Value } }) |
      ConvertTo-Json

The above yields:

{
  "Object2": {
    "name": "asdf2",
    "criteria": 1
  }
}

Upvotes: 4

Related Questions