Here is my Data.json. It has multi-level array. I must get all array elements:
{
"host": "http://localhost:5000",
"dlls": [
{
"files": [
{
"path": ".\\Xml.dll",
"store": ".\\DX\\OpenXml.dll"
},
{
"path": ".\\Xml.dll",
"store": ".\\DX\\OpenXml.dll"
},
{
"path": ".\\Xml.dll",
"store": ".\\DX\\OpenXml.dll"
},
{
"path": ".\\Xml.dll",
"store": ".\\DX\\OpenXml.dll"
},
{
"path": ".\\Xml.dll",
"store": ".\\DX\\OpenXml.dll"
}
]
},
{
"json": [
{
"path": ".\\index.json",
"store": ".\\DX\\index.json"
},
{
"path": ".\\index.json",
"store": ".\\DX\\index.json"
},
{
"path": ".\\index.json",
"store": ".\\DX\\index.json"
},
{
"path": ".\\index.json",
"store": ".\\DX\\index.json"
},
{
"path": ".\\index.json",
"store": ".\\DX\\index.json"
},
{
"path": ".\\index.json",
"store": ".\\DX\\index.json"
}
]
}
]
}
In my current PS script, I can only iterate one level:
$json = $null;
$jsonparsed = $null;
$validJson = $false;
try {
$json = Get-Content -Raw $file;
$jsonparsed = ConvertFrom-Json $json -ErrorAction Stop;
$validJson = $true;
} catch {
$validJson = $false;
}
if ($validJson) {
Write-Host "Provided text has been correctly parsed to JSON";
Write-Host $jsonparsed;
} else {
Write-Host "Provided text is not a valid JSON string" -ForegroundColor "Red";
return;
}
I have to parse all JSON array element. Retrieve the value of each "path" and "store". Please te me how can I do it in PowerShell version 5. I found solutions by loading third-party assembly. But I'm not allowed to use any external assembly. Is it parseable without external assembly?
ConvertFrom-Jsonreturn aPSCustomObject? Just parse the members.$jsonparsed | flatten, see: Flatten-Object: powersnippets.com/flatten-objectConvertFrom-Jsonsince PowerShell v3 IIRC. That hasn't changed in v5. Are you experiencing a problem? If so, please describe the behavior you expect and what (exactly) doesn't work that way.$jsonparsed.dlls.files[0].pathwould be more accurate, although$jsonparsed.dlls.files.path[0]should work too in PowerShell v3 or newer, due to member enumeration.