1

I have a large JSON file (vehicle data) from which I want to read 1 specific value (vehicle identification number, for example). So, following this answer, I have this code with debug output:

$json = Get-Content $responsefilepath | ConvertFrom-Json
Write-Host $json
Write-Host $json.values | Where-Object key -eq "coc_VIN"
  1. The first line puts my inputs file into an object;
  2. the second line outputs the content of my file:
    enter image description here
  3. the third line should provide the value YV1DZ8256D2390218, but it's just blank.
  4. On the PS command line, this provides a nice table of all values:
    Get-Content .\data\02-returned\sample.json | ConvertFrom-Json
    enter image description here
  5. I would extend that command to filter on the desired key:
    Get-Content .\data\02-returned\sample.json | ConvertFrom-Json | Where-Object key -eq "coc_VIN"
    and again, that returns a blank line. Why?

Input file:enter image description here

0

1 Answer 1

2

I figured it out myself:

Where-Object key presumes that there is a key/value pair where the name of the key is literally key. My data does not have that, so that's why it does not work.

Instead, I can use the dot notation to select the value of the key named "coc_VIN":

$json = Get-Content $responsefilepath | ConvertFrom-Json
$myvalue = $json.coc_VIN

...and that works!

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

1 Comment

Please consider marking this as the solution to the question (I think you have to wait 24 hours for checking your own answers as solutions).

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.