Read in a Json File in powershell, and get values out of it

Viewed 771

I try to extract data from a json file i imported into powershell, the import part works fine for me, i am also able to see my output, but i want to get only the date of the Categorie "v", i have no idea how to figure it out. i tried something like:

$vPSObject.v.getValue(0)

but this dont work for me.

by code looks like:

$file= Get-Content "C:\Users\xxxx\OneDrive\Desktop\test.json"   

#write-host $file

$file= $file -split '"d":'


$file2=$file 

$file=$file2 | ConvertFrom-Json



$vPSObject= $file2 | ConvertFrom-Json 



$vPSObject.v

$vPSObject.n

my output looks like:

t n        v                                                                                                                                                                                
- -        -                                                                                                                                                                                
d 000064s2 {10.05.2021, , , testfile (user)}                                                                                                           
d 00006deCE {06.11.2020, , , testfile2}                                                                                                              
d 00006dasdCA {06.11.2020, , , testfile3 (user2)}     

my output should look like:

000064s2 10.05.2021
00006deCE 06.11.2020                                                                                                            
00006dasdCA 06.11.2020    

so i could store it into a array to work with it later.

part of my json file:

{
      "t": "d",
      "n": "000061B6",
      "v": [
        "20.07.2016",
        "",
        "",
        "test"
      ]
    },
2 Answers

Assuming you really want to create an array of strings each of which is composed of property values joined with a space, use the following:

(ConvertFrom-Json (Get-Content -Raw C:\Users\xxxx\OneDrive\Desktop\test.json)) | 
  ForEach-Object { '{0} {1}' -f $_.n, $_.v[0] }

You can prepend [array] $strings = to the command above in order to collect all resulting strings.

If, by contrast, you want to create objects with the properties of interest, see Santiago Squarzon's helpful answer.

Assuming your json looks like this when converted to object[]:

t n           v                     
- -           -                     
d 000061B6    {10.05.2021, , , test}
d 00006deCE   {06.11.2020, , , test}
d 00006dasdCA {06.11.2020, , , test}

You can use a calculated property to get the result you want (this is assuming that the Date on the v property is always the position 0 of the array):

$json | Select-Object n, @{n='v';e={$_.v[0]}}

Output:

n           v         
-           -         
000061B6    10.05.2021
00006deCE   06.11.2020
00006dasdCA 06.11.2020

After converting this output to json:

[
    {
        "n":  "000061B6",
        "v":  "10.05.2021"
    },
    {
        "n":  "00006deCE",
        "v":  "06.11.2020"
    },
    {
        "n":  "00006dasdCA",
        "v":  "06.11.2020"
    }
]

$newJson = $json | Select-Object n, @{n='v';e={$_.v[0]}}

PS /> $newJson.n
000061B6
00006deCE
00006dasdCA

PS /> $newJson.v
10.05.2021
06.11.2020
06.11.2020

PS /> $newJson.v[0]
10.05.2021
Related