0

I am trying to use the Invoke-RestMethod command to grab the XML response from a SOAP endpoint and convert it to JSON text. From what I have read, the Invoke-RestMethod should be converting the response to a custom object for me so this should be really simple... This is what I am trying....

$APIRequest = Invoke-RestMethod -Method Post -Uri $SOAPEndpointURL -Headers $requestHeader -Body $requestBody
$JSONResponse = ConvertTo-Json $APIRequest.Envelope -Depth 9

But this is what i get as the resulting JSON text?

[ [ [ [ [ [ [ [] ], [], [ [] ], [], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [], [], [ [] ], [ [] ], [ [] ], [ [] ], [], [ [] ], [ [] ], [ [] ], [ [] ], [], [ [] ], [ [] ], [ [] ], [ [] ] ] ], [ [] ], [] ] ] ] ]

Can anyone suggest a standard way to do this without trying to interpret the XML response manually as XML text?

If I update my request to create an output file too then I can see the proper XML response in the file.

$APIRequest = Invoke-RestMethod -Method Post -Uri $SOAPEndpointURL -Headers $requestHeader -Body $requestBody #-OutFile "C:\SOAPResponse.txt"

If I change my conversion to not have the "-Depth 9" then the result is now this which is confusing?

[ [ [ "System.Xml.XmlElement" ] ] ]

To provide more detail, this is what the XML looks like when I call the end point using PostMan.

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <GetCustomerListResponse>
            <GetCustomerListResult>
                <Data>
                    <Customer>
                        <AddressLine1>123 Yellow Street</AddressLine1>
                        <AddressLine2/>
                        <AddressPostCode>1234</AddressPostCode>
                        <AddressState/>
                        <AddressSuburb>Sydney</AddressSuburb>
                        <Email>[email protected]</Email>
                        <PgnRowNo>1</PgnRowNo>
                    </Customer>
                </Data>
                <Error>0</Error>
                <ErrorMessage i:nil="true"/>
            </GetCustomerListResult>
        </GetCustomerListResponse>
    </s:Body>
</s:Envelope>
3
  • Did you try [xml]$APIRequest = ..... before converting to Json? Commented Aug 13, 2021 at 3:16
  • 1
    "Can anyone suggest a standard way to do this without trying to interpret the XML response manually as XML text?" - There is no magic "do what I mean" conversion. Show the SOAP response and the JSON you would derive from that. Commented Aug 13, 2021 at 8:15
  • Would this help? Commented Aug 14, 2021 at 12:02

2 Answers 2

1

PowerShell doesn't offer direct conversion of XML to JSON out of the box.

You'll need to process the XML returned by the API and turn it into one of the types ConvertTo-Json can handle, like hashtable.

See an example below:

# fetch XML
$xmlData = Invoke-RestMethod https://www.nasa.gov/rss/dyn/breaking_news.rss

# extract fields of interest
$hashtables = $xmlData | %{ @{ Id = $_.Identifier; Text = $_.InnerText } }

# convert hashtables into JSON
$hashtables | ConvertTo-Json
Sign up to request clarification or add additional context in comments.

3 Comments

As far as I can tell your "$xmlData" is actually not an XML object but an already deserialized PowerShell Object[]'. ($xmlData.GetType()`)
@iRon, that behavior depends on what kind of XML is returned. In my example it was an RSS feed for which Invoke-RestMethod provides special handling which builds an array of System.Xml.XmlElement objects
I need to extract data from various end points so would prefer to not have to specifically identify fields of interest. Just a complete conversion from XML to JSON...
1

Using the newtonsoft serializer.
(Depending on your system/PowerShell version newtonsoft might already been available, otherwise see: How do I use Json.NET to parse json in PowerShell?)

$Xml = [Xml]@"
<?xml version="1.0" encoding="utf-8"?>
<Book>
  <projects>
    <project name="Book1" date="2009-01-20">
      <editions>
        <edition language="English">En.Book1.com</edition>
        <edition language="German">Ge.Book1.Com</edition>
        <edition language="French">Fr.Book1.com</edition>
        <edition language="Polish">Pl.Book1.com</edition>
      </editions>
    </project>
  </projects>
</Book>
"@

[Newtonsoft.Json.JsonConvert]::SerializeXmlNode($Xml, 1)
{
  "?xml": {
    "@version": "1.0",
    "@encoding": "utf-8"
  },
  "Book": {
    "projects": {
      "project": {
        "@name": "Book1",
        "@date": "2009-01-20",
        "editions": {
          "edition": [
            {
              "@language": "English",
              "#text": "En.Book1.com"
            },
            {
              "@language": "German",
              "#text": "Ge.Book1.Com"
            },
            {
              "@language": "French",
              "#text": "Fr.Book1.com"
            },
            {
              "@language": "Polish",
              "#text": "Pl.Book1.com"
            }
          ]
        }
      }
    }
  }
}

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.