1

I'm trying to use Zabbix JSON API to automate some monitoring stuff in our IT shop. I'd like to use the graph.create method described here : https://www.zabbix.com/documentation/2.2/manual/api/reference/graph/create

I'm struggling with the gitems array. It must contain hash tables (one per item in the graph), each with "itemid" and "color" rows.

This is part of my code :

#i get a list of itemids in $items
$colours=@("C04000", "800000", "191970", "3EB489", [...])
$params=@{}
        $gitems=@() #an array of hash tables...

        $c=0
        foreach ($itemid in $items.result) {
            $graphmember=@{}
            $graphmember.add("itemid", $itemid)
            $graphmember.add("color", $colours[$c])
            $gitems += $graphmember
            $c += 1
        }
        $params.add("gitems", $gitems)

        #construct the JSON object  
        $objgraph = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
        Add-Member -PassThru NoteProperty method 'graph.create' |
        Add-Member -PassThru NoteProperty params $params |
        Add-Member -PassThru NoteProperty auth $session.result |
        Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json

        return $objgraph

Which, when called returns this :

{
    "jsonrpc":  "2.0",
    "method":  "graph.create",
    "params":  {
                   "gitems":  [
                                  "System.Collections.Hashtable",
                                  "System.Collections.Hashtable",
                                  "System.Collections.Hashtable",
                                  "System.Collections.Hashtable",
                                  "System.Collections.Hashtable"
                              ]
               },
    "auth":  "dc50acf4c337e5430c00936f998f74da",
    "id":  "2"
}

So i get 5 rows, which is the right number based on the arguments i supplied, but it seems like convertto-json doesnt like my object... can't figure out why.

I was not sure about the hash table in a array thing so i did a test and it seems to work :

$gitems=@()
$i1=@{}
$1.add("itemid","123")
$i1.add("color","blue")
$gitems += $i1
$i2=@{}
$i2.add("itemid","567")
$i2.add("color","yellow")
$gitems += $i2

$gitems

Name                           Value
----                           -----
color                          bleu
itemid                         123
color                          yellow
itemid                         567

Thanks for ideas people!

simsaull
  • 319
  • 1
  • 4
  • 12
  • Seems similar to [this](http://stackoverflow.com/questions/17929494/powershell-v3-convertto-json-with-embedded-hashtable) – jon Z Aug 16 '13 at 14:20

1 Answers1

2

The depth parameter specifies how many levels of contained objects are included in the JSON representation. The default value is 2. If you specify a value 3, the json will be created successfully:

$objgraph = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'graph.create' |
Add-Member -PassThru NoteProperty params $params |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json -depth 3
jon Z
  • 15,838
  • 1
  • 33
  • 35