Nested Arrays and ConvertTo-Json

To use the REST API, I have to pass a JSON object that looks like this:

{ "series" : [{ "metric": "custom.powershell.gauge", "points":[[1434684739, 1000]] } ] } 

Pay attention to the nested array here. I can not reproduce this. Here is my code:

 [int][double]$unixtime=get-date ( (get-date).ToUniversalTime() ) -UFormat %s $obj=@ {} $series=@ {} $array=@ () $points=@ () $value=get-random -Minimum 0 -Maximum 100 $series.add("metric","custom.powershell.gauge") $points=@ (@($unixtime, $value)) $series.add("points",$points) $obj.Add("series",@($series)) $json=$obj | ConvertTo-Json -Depth 30 -Compress $json 

And here is the conclusion:

 {"series":[{"points":[1434685292,95],"metric":"custom.powershell.gauge"}]} 

I tried a lot of things, I can not get 2 arrays to be nested, it always looks like a separate array.

In the same note, someone explained this, please:

 > $a=(1,2) > $a 1 2 > $a | ConvertTo-Json [ 1, 2 ] > $b=($a,$a) > $b 1 2 1 2 > $b | ConvertTo-Json [ { "value": [ 1, 2 ], "Count": 2 }, { "value": [ 1, 2 ], "Count": 2 } ] 

Where are these value and Count ?

Thank you for your help.

+6
source share
3 answers

The explanation is that (1,2),(3,4 ) is an array of the array, but Powershell shares the first level on the pipe | , and you are not giving a name for these arrays, so the serializer supplies it. First try:

 # First build your array of array $z = (1,2),(3,4) # convert it to JSON using the , ,$z | ConvertTo-Json -Depth 5 -Compress [psobject]@{"points"=$z} | ConvertTo-Json -Depth 5 -Compress 

He gives the first step:

 {"value":[[1,2],[3,4]],"Count":2} {"points":[[1,2],[3,4]]} 

Now the solution I propose :

 # First build your array of array $z = (1,2),(3,4) # Then build a PSCustom object $a = [pscustomobject]@{"series" = ,@{"metric"="custom.powershell.gauge"; "points"=$z}} # At the end convert it to JSON # don't forget the **Depth** parameter (use **Compress** to retreive one line like above) $a | ConvertTo-Json -Depth 5 

For me, this gives something close to what you need:

 { "series": [ { "points": [ [ 1, 2 ], [ 3, 4 ] ], "metric": "custom.powershell.gauge" } ] } 
+9
source

Late to the party, but I would like to offer a more visually intuitive solution that is easy to expand (I, like others, am a visual student, so code blocks like the one below help me understand things easier):

 [int][double]$unixtime = Get-Date ((Get-Date).ToUniversalTime()) -UFormat %s $value = Get-Random -Minimum 0 -Maximum 100 $body = @{ 'series' = @( [Ordered]@{ 'metric'='custom.powershell.gauge' 'points' = @( ,@($unixtime,$value) ) } ) } ConvertTo-Json -InputObject $body -Depth 4 

Outputs:

 { "series": [ { "metric": "custom.powershell.gauge", "points": [ [ 1473698742, 96 ] ] } ] } 

-Depth 4 gets an extra set of square brackets around your point values, and [Ordered] ensures that the hash table is ordered as shown in its original form. Do not forget -Compress before sending, as others have said.

+2
source

As JPBlanc suggested, creating a custom object. Below is my code:

 [long]$value=Get-Random -Minimum 0 -Maximum 100 $points=,@($unixtime, $value) $metricname="custom.powershell.gauge" $obj = [pscustomobject]@{"series" = ,@{"metric" = $metricname; "points"=$points}} $json=$obj | ConvertTo-Json -Depth 5 -Compress 

What outputs:

 {"series":[{"points":[[1434810163,53]],"metric":"custom.powershell.gauge"}]} 

Remember to specify a depth> 2.

Thanks!

+1
source

Source: https://habr.com/ru/post/989374/


All Articles