masiboo
masiboo

Reputation: 4719

How to parse multi-level JSON array?

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?

Upvotes: 1

Views: 1759

Answers (1)

iRon
iRon

Reputation: 23663

If you paste this into a PowerShell (v5) window:

$jsonparsed = convertFrom-Json @'
{
  "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"        
        }
      ]
    }
  ]
}
'@

Write-Host $jsonparsed.host
ForEach ($dll in $jsonparsed.dlls) {
    ForEach ($file in $dll.files) {
        Write-Host $file.path
        Write-Host $file.store
    }
    ForEach ($json in $dll.json) {
        Write-Host $json.path
        Write-Host $json.store
    }
}

You should get this:

.\Xml.dll
.\DX\OpenXml.dll
.\Xml.dll
.\DX\OpenXml.dll
.\Xml.dll
.\DX\OpenXml.dll
.\Xml.dll
.\DX\OpenXml.dll
.\Xml.dll
.\DX\OpenXml.dll
.\index.json
.\DX\index.json
.\index.json
.\DX\index.json
.\index.json
.\DX\index.json
.\index.json
.\DX\index.json
.\index.json
.\DX\index.json
.\index.json
.\DX\index.json

Upvotes: 1

Related Questions