2

This is my file structure, I would Like to access Subscription base on the value of "Name"

{                                                                    
 "Topics": 
 [
     {
         "Name": "topic1",
         "Subscription": "sub1"
     },
     {
         "Name": "topic2",
         "Subscription": "sub2"
     }
 ]

}

$json = Get-Content 'path' | Out-String | ConvertFrom-Json
$subscriptions=$json.Topics.Subscription|Where Name -EQ "topic1" 

I'm getting the Name by executing following command

$json.Topics.Name
    topic1
    topic2
$json.Topics.Subscriptions
    sub1
    sub2

But not sure how to keep this in where clause

0

1 Answer 1

1

Try the following:

$json = @'
{
    "Topics":
    [
        {
            "Name": "topic1",
            "Subscription": "sub1"
        },
        {
            "Name": "topic2",
            "Subscription": "sub2"
        }
    ]

}
'@

$objectFromJson  = $json | ConvertFrom-Json
$objectFromJson.Topics | Where-Object Name -eq 'topic1'

The above yields:

Name   Subscription
----   ------------
topic1 sub1

If you would like to output only the .Subscription property value, simply use
($objectFromJson.Topics | Where-Object Name -eq 'topic1').Subscription


As for what you tried:

$json.Topics.Subscription

This extracts the values of the Subscription properties, which are mere strings ("sub1" and "sub2"), which don't have a .Name property, so your Where Name -EQ "topic1" filter matches nothing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.