0

I have a Json file with combination of strings, substrings and array as attached below.

I tried to fetch the Id using the psobject and reading the JSON file as RAW but didnt workout.

$json = (Get-Content "C:\Users\rasto\OneDrive\Desktop\Job_board\output.json" -Raw) | ConvertFrom-Json

$json.psobject.TypeNames.item

Further I tried to run loop but didnt work as well.

$data = Get-Content "Path to json file" | Out-String | ConvertFrom-Json

foreach ($line in $data) {
     $line.id
}

Json File:

{
    "data":  [
                 {
                     "id":  "146993",
                     "text":  "value1"
                 },
                 {
                     "id":  "14699",
                     "text":  "value2"
                 }
             ],
    "meta":  {
                 "newest_id":  "146",
                 "oldest_id":  "146",
                 "result_count":  79
             }
}

I want to extract the values in field "id" using PowerShell.

Output:

146993 14699

0

1 Answer 1

1

$data is not an array, so looping over it won't make any difference. You need to dereference the data member of the the root object to get to the array that has objects with the id property:

$data = Get-Content "Path to json file" |ConvertFrom-Json
$IDs = $data.data.id
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.