0

I'm struggling with filtering the sessionId from the following output:

status  messages                           data
------  --------                           ----
Success {@{code=success; message=Success}} @{sessionId=0662c4d429bb51ef772e875f9c9b3a46; faSessionId=qfvio0382283ihbknhfh70kvn4; phpSessionId=qfvio0382283ihbknhfh70kvn4}

I've tried with the following:

$sessionId = $response | select "data"

This is returning:

data
----
@{sessionId=f77d5bfb5bdc65163e605d9c7edbcaed; faSessionId=hrsbd5iuq8i6dttfhimpm5baf1; phpSessionId=hrsbd5iuq8i6dttfhimpm5baf1}

I can't get it working to only extract the sessionId part.

It would be great if anyone could assist with the correct PowerShell code to obtain this.

2
  • 1
    $sessionId = $response.data.sessionId Commented Dec 18, 2020 at 14:29
  • @RoadRunner has the right solution. The 'data' property is just another PowerShell object which has properties of it's own. Commented Dec 18, 2020 at 14:34

1 Answer 1

2

It might help to think of the object you're working with like this, in JSON.

{
    "Status":  "Success",
    "Messages":  {
                     "code":  "success",
                     "message":  "Success"
                 },
    "Data":  {
                 "faSessionId":  "qfvio0382283ihbknhfh70kvn4",
                 "phpSessionId":  "qfvio0382283ihbknhfh70kvn4",
                 "sessionId":  "0662c4d429bb51ef772e875f9c9b3a46"
             }
}

You want the sessionId propeerty so you're selecting Data, but then you're finding that Data actually has those other properties of phpSessionId and faSessionId. Fortunately you have two approaches to get what you need.

Continue using the Select-Object cmdlet

You are already using this cmdlet but using the alias of Select, so you can keep using Select-Object to drill down all the way to the center of the Earth basically.

$response | Select-Object -ExpandProperty Data | Select-Object -ExpandProperty sessionId
>0662c4d429bb51ef772e875f9c9b3a46

Drill into the property you want with Dereferencing

This is the more programmery way to do it but is very popular. It's also called dot-notation.

$response.Data.sessionId
0662c4d429bb51ef772e875f9c9b3a46
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.