2

I'm quite new to Powershell and currently I'm trying to convert some data from JSON I downloaded from Solr.

The JSON file looks like:

{
 "responseHeader": {"status":0, "QTime":2651},
"InitFailures":{}, 
"status":{
   "YX_SERVER.ONE_LOG.2020-11-07_07_49_33": {
         "name":"SERVER.ONE_LOG.2020-11-07_07_49_33",
            "index":{
               "sizeInBytes":69,
                "size":"69 bytes"}},
 "XY_SERVER.TWO_LOG.2020-11-08_09_52_11": {
         "name":"XY_SERVER.TWO_LOG.2020-11-08_09_52_11",
            "index":{
               "sizeInBytes":6487,
                    "size":"6487 bytes"}},
 "ZX_SERVER.THREE_LOG.2020-10-07_07_42_23": {
         "name":"SERVER.THREE_LOG.2020-10-07_07_42_23",
            "index":{
               "sizeInBytes":6977,
               "size":"6977 bytes"
                              }}}}

Below the second status line is the list of the servers with numerous parameters(all names are different), I tried to go deeper into it, but need help with nested data.

I tried the following:

$list = Get-Content C:\Users\User\Download\cores.json - Raw |convertfrom-json |select-object -ExpandProperty status

Then $list shows me the list of servers and the data needed is in the right column but I cannot access it directly. How can I build the table and calculate the sum just like follows:

Name                                     Size(inbytes) 
YX_SERVER.ONE_LOG.2020-11-07_07_49_33    69
XY_SERVER.TWO_LOG.2020-11-08_09_52_11    6487
ZX_SERVER.THREE_LOG.2020-10-07_07_42_23  6977

Summary                                  Total size
                                         ?
0

1 Answer 1

1
$Content = Get-Content .\cores.json
$Data = $Content | ConvertFrom-Json
$Status = $Data.status
$Table = $Status.PSObject.Properties.Value | Foreach-Object {
    [pscustomobject]@{
        name = $_.name
        sizeInBytes = $_.index.sizeInBytes
    }
}
$Table

[pscustomobject]@{
    name = 'Total Size:'
    sizeInBytes = ($Table.sizeInBytes | Measure-Object -Sum).Sum
}

name                                  sizeInBytes
----                                  -----------
SERVER.THREE_LOG.2020-10-07_07_42_23         6977
XY_SERVER.TWO_LOG.2020-11-08_09_52_11        6487
SERVER.ONE_LOG.2020-11-07_07_49_33             69
Total Size:                                 13533
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry for that typo in JSON! I edited and validated it. Unfortunately -ashashtable option was introduced in Powershell 6.0. And I only have 5.0 installed. Is there any other way to do this?
I have updated the answer for older versions of PowerShell that do not support -AsHashTable in which case you might simply enumerate the $Status.PSObject.Properties.Value member
That perfectly helped! Thank YOU so MUCH! You're amazing! ^__^

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.