1

I have a dataset consisting of a two-dimensional array (flexData[5][2]). I have this defined in my Powershell script as follows:

class flexData {
    [DateTime]$dateTime
    [string]$firmwareVersion
    [string[][]]$flexData
}
$flexObj = [flexData]@{dateTime = $(Get-Date); firmwareVersion = 'u031C'; flexData = @(@(0, 1), @(320, 17), @(45, 36), @(0, 0))}

The problem with this is that the output object that ConvertTo-Json spits out is hard to read:

{
  "dateTime": "2021-10-11T13:58:25.0937842+02:00",
  "firmwareVersion": "u031C",
  "flexData": [
    [
      "0",
      "1"
    ],
    [
      "320",
      "17"
    ],
    [
      "45",
      "36"
    ],
    [
      "0",
      "0"
    ]
  ]
}

Is there a way to instead of using a single key name and two-dimensional arrays, to instead convert this to flexData0, flexData1 ... flexData4 and keep my actual data as single-dimensional arrays? I could obviously do this by manually defining my class as:

class flexData {
    [DateTime]$dateTime
    [string]$firmwareVersion
    [string[]]$flexData0
    [string[]]$flexData1
    [string[]]$flexData2
    [string[]]$flexData3
    [string[]]$flexData4
}

But is there a smarter way of doing this? Especially since I would also like to make a third-dimension of my array to store multiple iterations of flexData?

2
  • "the output object that ConvertTo-Json spits out is hard to read" - is that important? I assume you convert it to JSON because you want another computer/program to read it later - and computers don't care about how aesthetically pleasing the serialization format is :) Commented Oct 11, 2021 at 12:13
  • Indeed, perhaps I should have phrased it better. Of course this will be used elsewhere in order to visualize the data and the format will not be a problem. However, if I could implement the functionality of separating a property into multiple simpler properties, it would drastically increase human-readability which is a great thing to have. Commented Oct 11, 2021 at 12:15

1 Answer 1

2

You could add a constructor to your flexData class that creates an object from the top-level array instead:

class flexData {
    [DateTime]$dateTime
    [string]$firmwareVersion
    [psobject]$flexData

    flexData([DateTime]$dateTime, [string]$firmwareVersion, [string[][]]$flexData){
        $this.dateTime = $dateTime
        $this.firmwareVersion = $firmwareVersion

        # Create object from nested array
        $dataProperties = [ordered]@{}
        for($i = 0; $i -lt $flexData.Length; $i++){
            $dataProperties["$i"] = $flexData[$i]
        }
        $this.flexData = [pscustomobject]$dataProperties
    }
}

Now, the individual outer array items will be listed as properties named 0 through (N-1):

PS ~> $data = [flexData]::new($(Get-Date), 'u031C', @(@(0, 1), @(320, 17), @(45, 36), @(0, 0)))
PS ~> $data |ConvertTo-Json
{
  "dateTime": "2021-10-11T14:21:48.4026882+02:00",
  "firmwareVersion": "u031C",
  "flexData": {
    "0": [
      "0",
      "1"
    ],
    "1": [
      "320",
      "17"
    ],
    "2": [
      "45",
      "36"
    ],
    "3": [
      "0",
      "0"
    ]
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This works great! I've also used a switch-case for the values of $i in order to get custom sub-object names. Thank you! :)

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.