1

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?

10
  • Doesn't ConvertFrom-Json return a PSCustomObject? Just parse the members. Commented Dec 18, 2017 at 17:25
  • What output do you expect instead? Commented Dec 18, 2017 at 17:57
  • Are you looking for this: $jsonparsed | flatten, see: Flatten-Object: powersnippets.com/flatten-object Commented Dec 18, 2017 at 18:02
  • Umm... what exactly is your question here? Parsing JSON is done with ConvertFrom-Json since 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. Commented Dec 18, 2017 at 18:43
  • 1
    @iRon $jsonparsed.dlls.files[0].path would be more accurate, although $jsonparsed.dlls.files.path[0] should work too in PowerShell v3 or newer, due to member enumeration. Commented Dec 19, 2017 at 0:07

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I don't work. It doesn't even go inside the ForEach top-level loop ForEach ($dll in $jsonparsed.dlls) . So I don't see anything
I pasted the code from @iRon into a new PowerShell window and it works perfectly fine.

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.