I have a json file, its content is:
{
"PID": [
18988,
23928,
16656
],
"Process": [
"pwsh.exe",
"pwsh.exe",
"WindowsTerminal.exe"
],
"WinID": [
"0x1117b6",
"0x130042",
"0x600848"
]
}
I want to import it as a PsCustom
object, I have tried Get-Content -Path c:tempMyJson.json|ConvertFrom-Json
but this imports the data as a nested object:
PID Process WinID
--- ------- -----
{18988, 23928, 16656} {pwsh.exe, pwsh.exe, WindowsTerminal.exe} {0x1117b6, 0x130042, 0x600848}
I am expecting the following:
PID Process WinID
--- ------- -----
18988 pwsh.exe 0x1117b6
23928 pwsh.exe 0x130042
16656 WindowsTerminal.exe 0x600848
I have restructured the Json file multiple times, I am not sure where I am going wrong. I am open to changing the json file itself.
I would much rather solve this solution at the json file itself, rather in PowerShell with something like ...ConverFrom-Json| Where-Oject...
, can someone share with me the correct json format to provide for PowerShell, in this case?
Any help would be greatly appreciated!
2
Answers
The JSON is imported as a single object with 3 properties:
PID
,Process
, andWinID
. Each of those properties contains 3 values. What you’re requesting is to have 3 different objects, each with thePID
,Process
, andWinID
properties and containing one of each of those values.If you control the JSON you’re capturing (I assume you do, since you’re pulling it from a file), the simplest solution would be to change the way you’re structuring the JSON to match what you expect:
If you can’t change the source JSON, this will turn what you have from one object into three:
Using the JoinModule from the PowerShell Gallery, you could dynamically (independently of the column names and sizes) join the properties list side-by-side:
See also: