irom
irom

Reputation: 3596

Getting type string after converting to JSON in PowerShell

Trying to convert string to JSON in PowerShell.

$resultsTable = $webresults.Content
Write-Host $resultsTable

Output:

{"tables":[{"name":"PrimaryResult","columns":[{"name":"ComputerIP","type":"string"}],"rows":[["40.121.111.160"],["104.41.134.153"]]}]}

but still getting System.String

$resultsTable = $webresults.Content | ConvertTo-Json
Write-Host $resultsTable
Write-Host $resultsTable.GetType()

Output:

"{\"tables\":[{\"name\":\"PrimaryResult\",\"columns\":[{\"name\":\"ComputerIP\",\"type\":\"string\"}],\"rows\":[[\"40.121.111.160\"],[\"104.41.134.153\"]]}]}"
System.String

Upvotes: 0

Views: 729

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

Trying to convert string to json in Powershell

As has been mentioned in the comments: your string is already a json document.

If you want to convert it to some structured objects you can work with, you'll have to use ConvertFrom-Json:

PS C:\> $object = $resultsTable |ConvertFrom-Json
PS C:\> $object.GetType().Name
PSCustomObject
PS C:\> $object |Get-Member

   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
tables      NoteProperty Object[] tables=System.Object[]

Upvotes: 3

Related Questions