3

I spend 3 hours debugging, but I don't get the rootcause.

I have text (in JSON Format) in a variable. I convert it with "Convert-From-JSON" to an actual object. I was hoping that I can then work with the properties of the object. But it does not work :( It seems that the conversion only gives me two tables (status and data). What am I doing wrong?

My code:

$results = {
  "status": "FINISHED",
  "data": {
    "results": [
      {
        "id": "11565C230500",
        "custom": {
          "image": "XXXX",
          "name": "XXXX",
          "articleNumber": "4032423423505"
        }
      },
      {
        "id": "22739L5F16243",
        "custom": {
          "image": "XXXX",
          "name": "XXXX",
          "articleNumber": "4032423423505"
        }
      },
      {
        "id": "3304332724004",
        "custom": {
          "image": "XXXX",
          "name": "XXXX",
          "articleNumber": "4032423423505"
        }
      }
    ]
   }
 }

Now putting it as Object:

$resultObject = ConvertFrom-JSON -InputObject $result

Now trying to retrieve:

$resultObject.id | where {$_.id -eq '11565C230500'}
2
  • The JSON you've posted is invalid (array literal following "results": is never closed, neither is the surrounding object) Commented Nov 4, 2021 at 15:21
  • the data posted by you is invalid Commented Nov 4, 2021 at 15:37

2 Answers 2

3

Assuming the json is single quoted in that example, otherwise it's a scriptblock, and "$results" is used in place of "$result", the id property is underneath data.results. You couldn't examine the id property with where-object if you already expanded it. That's an alternate form of where-object. I'm expanding the "custom" property with the foreach-object alias "%". Note one id number is shorter. Beware convertto-json's default depth of 2.

$results = '{"status":"FINISHED","data":{"results":[
{"id":"11565C230500", "custom":{"image":"XXXX","name":"XXXX","articleNumber":"4032423423505"}},
{"id":"22739L5F16243","custom":{"image":"XXXX","name":"XXXX","articleNumber":"4032423423505"}},
{"id":"3304332724004","custom":{"image":"XXXX","name":"XXXX","articleNumber":"4032423423505"}}
]}}'

$resultObject = ConvertFrom-JSON -InputObject $results
$resultObject.data.results | where id -eq 11565C230500

id           custom
--           ------
11565C230500 @{image=XXXX; name=XXXX; articleNumber=4032423423505}


$resultobject.data.results | where id -eq 11565C230500 | % custom

image name articleNumber
----- ---- -------------
XXXX  XXXX 4032423423505
Sign up to request clarification or add additional context in comments.

Comments

2

As it is now, you define $results as script block where it should be a string:

$results =  @'
{
    "status": "FINISHED",
    "data": {
        "results": [{
                "id": "11565C230500",
                "custom": {
                    "image": "XXXX",
                    "name": "XXXX",
                    "articleNumber": "4032423423505"
                }
            },
            {
                "id": "22739L5F16243",
                "custom": {
                    "image": "XXXX",
                    "name": "XXXX",
                    "articleNumber": "4032423423505"
                }
            },
            {
                "id": "3304332724004",
                "custom": {
                    "image": "XXXX",
                    "name": "XXXX",
                    "articleNumber": "4032423423505"
                }
            }
        ]
    }
}
'@

Now you can convert it from the JSON string

$resultObject = $results | ConvertFrom-Json

Then, to parse out the nested property, you need to follow the data structure:

$resultObject.data.results | Where-Object {$_.id -eq '11565C230500'}

will give you this:

id           custom                                               
--           ------                                               
11565C230500 @{image=XXXX; name=XXXX; articleNumber=4032423423505}

If you want to get the articleNumber from the custom object that contains, dig deeper still:

($resultObject.data.results | Where-Object {$_.id -eq '11565C230500'}).custom.articleNumber

which will return 4032423423505

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.