> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Dynamic XML-2-JSON Script

> Dynamically converting parent node and all child nodes specified in XML to JSON format with Groovy script

<Info>
  The Rest-2-Soap feature on Apinizer can be used to expose an XML/SOAP web service as JSON/REST. For detailed information, you can review the [SOAP/REST API Proxy Creation](/en/develop/api-proxy-creation/soap-rest-api-proxy-creation) page.
</Info>

## Groovy Script

By entering the name of the parent node you want to filter in the relevant XML in the `"nodeName"` field, you can dynamically convert that node and all its child nodes to JSON.

```groovy theme={null}
import groovy.util.XmlSlurper
import groovy.json.JsonOutput

try {
    def slurper = new XmlSlurper(false, false)
    def data = slurper.parseText(requestBodyTextFromClient)
    def parsedData = data.'**'.findAll { it.name().contains('nodeName') }
    def result = [:]
    parsedData.each { node ->
        node.'**'.each { subNode ->
            if (!subNode.children() || subNode.children().size() == 0) {
                result[subNode.name()] = subNode.text()
            }
        }
    }
    requestBodyTextFromClient = JsonOutput.prettyPrint(JsonOutput.toJson(result))
} catch (Exception ex) {
    requestErrorMessageToTargetAPI = ex.message
}
```

## Explanation

This script performs the following operations:

1. **XML Parse**: XML data coming from request body is parsed
2. **Node Filtering**: Parent node and all child nodes matching the specified `nodeName` are found
3. **JSON Conversion**: Found nodes are converted to JSON format
4. **Error Management**: Error message is created in case of error

<Note>
  This script should be run on the request line (Request Policy) because it uses the `requestBodyTextFromClient` and `requestErrorMessageToTargetAPI` variables.
</Note>
