0

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
1
  • 4
    The String is json so you should be converting From not To, use the ConvertFrom-Json cmdlet Commented Nov 17, 2019 at 12:39

1 Answer 1

3

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[]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.