locked
wrap a hashtable into an array with values rather than the object. RRS feed

  • Question

  • bear with me, this is gonna take some explaining!

    I need to write some code which allows me to submit to azure machine learning, for testing usage.

    the format is 

    dictionary, containing an array of dictionaries which are the use cases.

    so the following code

    # create the usage case to submit
    $usecase1 = @{
        "Father" = "dad";
        "Mother" = "mum";
    }
    
    # its possible to submit multiple so wrap in an array
    $inputArray =@()
    $inputArray += $usecase1
    
    # create the dictionary, add the array in
    $inputsDictionary = @{}        
    $inputsDictionary.Add("input1", $input1Array) 
    
    # convert to json for submission
    $data = $inp | ConvertTo-Json
    $data 
    
    invoke-webrequest -Method POST -Uri $uri -Headers $headers -Body $data -ContentType 'application/json' -verbose


    when we run this, it outputs the data object as 

    {
        "inputs":  {
                       "input1":  [
                                      "System.Collections.Hashtable"
                                  ]
                   }
    }

    which isnt right. i need to insert the hashtable as values.

    anyone seen this before or have an example of how to do it that they can link me to?

    Tuesday, September 5, 2017 7:51 AM

Answers

  • The first thing every good coder learns is to always check the help before asking a question:

    help ConvertTo-Json -par Depth


    \_(ツ)_/

    • Marked as answer by mattshort77 Tuesday, September 5, 2017 9:58 AM
    Tuesday, September 5, 2017 8:13 AM

All replies

  • The first thing every good coder learns is to always check the help before asking a question:

    help ConvertTo-Json -par Depth


    \_(ツ)_/

    • Marked as answer by mattshort77 Tuesday, September 5, 2017 9:58 AM
    Tuesday, September 5, 2017 8:13 AM
  • Hi,

    I can't reproduce your issue, since I get this output:

    {
        "input1":  [
                       {
                           "Father":  "dad",
                           "Mother":  "mum"
                       }
                   ]
    }
    With your code (some typos corrected):
    $usecase1 = @{
        "Father" = "dad";
        "Mother" = "mum";
    }
    
    # its possible to submit multiple so wrap in an array
    $inputArray =@()
    $inputArray += $usecase1
    
    # create the dictionary, add the array in
    $inputsDictionary = @{}        
    $inputsDictionary.Add("input1", $inputArray) 
    
    # convert to json for submission
    $inputsDictionary | ConvertTo-Json

    Tuesday, September 5, 2017 8:14 AM
  • Yes - there are mistakes.  The code and question do not match.  Your code sample does not reproduce your issue.  Your stated error result contains an "input:" key but that does not exist in the code.


    \_(ツ)_/


    • Edited by jrv Tuesday, September 5, 2017 8:22 AM
    Tuesday, September 5, 2017 8:20 AM
  • I thought the issue was with the way I built the array, so I was looking (and reading help) in the wrong area. 
    Tuesday, September 5, 2017 9:58 AM