Query for input then output in json file

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Daniel Krebs Daniel Krebs 1 year, 9 months ago.

  • Author
    Posts
  • #24271
    Profile photo of bvi1998 .
    bvi1998 .
    Participant

    Hi, I want to get data:

    $IP = Read-host "IP"
    $loc = Read-host "Location"
    $notes = Read-host "Notes"

    Then I want to put the variables into a json file which I will use as input to an API. I have having trouble with accomplishing this, can someone help please? ThankS!

    $json = @'
    {
    "address": ["$ip"],
    "worker": "$loc",
    "notes": "$notes"
    }
    '@ | out-file C:\temp\temp.jason

  • #24273
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

    $IP = Read-host "IP"
    $loc = Read-host "Location"
    $notes = Read-host "Notes"
    $json = @{"address"=$ip;
    "worker"=$loc;
    "notes"=$notes
    }
    $obj = New-Object -TypeName PSObject -Property $json
    $obj | ConvertTo-Json | Out-File -FilePath C:\temp\temp.json

  • #24280
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Alternative version which does not use Read-Host and New-Object:

    Param (
        [Parameter(Mandatory)]
        $IPAddress,
    
        [Parameter(Mandatory)]
        $Location,
    
        [Parameter(Mandatory)]
        $Notes,
    
        $OutputFilePath = 'C:\TEMP\My.json'
    )
    
    @{
        "address" = $IPAddress
        "worker" = $Location
        "notes" = $Notes
    
    } | ConvertTo-Json | Out-File -FilePath $OutputFilePath
    
  • #24301
    Profile photo of bvi1998 .
    bvi1998 .
    Participant

    Thanks all – the reason I had the brackets was that it is required by the API. This would be the required command line input if I were to just run it (I don't need all of the variables listed though)

    {"hosttype":"server", "address": ["127.0.0.1"], "parents": [], "tag": ["demo"], "umiworker": "worker", "notes": "this is a test", "url": "http://www.tblahbah.com"}

    obviously the brackets are for a list, I guess an array....

    Thanks!

  • #24307
    Profile photo of bvi1998 .
    bvi1998 .
    Participant

    sorry, I made an error in my first post. The input file will be:

    $json = @{"address": [$ip];\,
    "worker" : $loc,
    "tag" : [$tag]
    "notes" : $notes
    }

    If I do this, I get an error:
    At line:1 char:20
    + $json = @{"address": [$ip];\,
    + ~
    Missing '=' operator after key in hash literal.

    can someone help, thanks!

  • #24311
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    You are converting Powershell to JSON. In Powershell, a hashtable is being generated with the data to convert to JSON. The format you provided is not a hashtable, which is why you are getting an error. If you just want to wrap a value in square brackets, you could do something like this:

    $IPAddress = "10.10.10.10"
    $Location = "Location A"
    $tag = "test"
    $notes = "Some notes"
    
    
    $hashTable = @{
        "address" = "[{0}]" -f $IPAddress;
        "worker" = $Location;
        "tag" = $tag;
        "notes" = $Notes;
    } 
    
    $hashTable | ConvertTo-JSON
    

    Which would produce JSON like this:

    {
        "worker":  "Location A",
        "tag":  "test",
        "address":  "[10.10.10.10]",
        "notes":  "Some notes"
    }
    

    You just need to make sure that Powershell is formatted as a hashtable. You can manipulate the column or the value, but not the format of the hashtable. Hopefully this will help.

  • #24316
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    I think you are looking for something like below:

    Param (
    
        [Parameter(Mandatory)]
        [String[]]
        $IPAddress,
     
        [Parameter(Mandatory)]
        [String]
        $Worker,
     
        [Parameter(Mandatory)]
        [String[]]
        $Tag,
    
        [Parameter(Mandatory)]
        [String]
        $Notes,
     
        $OutputFilePath = 'C:\TEMP\My.json'
    )
     
    [ordered]@{
        "address" = $IPAddress
        "worker" = $Worker
        "tag" = $Tag
        "notes" = $Notes
     
    } | ConvertTo-Json
    

    Input

    IPAddress[0]: 10.0.0.1
    IPAddress[1]: 10.0.0.2
    IPAddress[2]: 
    Worker: Location A
    Tag[0]: Self
    Tag[1]: help
    Tag[2]: 
    Notes: Important
    

    JSON output

    {
        "address":  [
                        "10.0.0.1",
                        "10.0.0.2"
                    ],
        "worker":  "Location A",
        "tag":  [
                    "Self",
                    "help"
                ],
        "notes":  "Important"
    }
    

You must be logged in to reply to this topic.