0
xml = """
<Header_A>
        <Child_A></Child_A>
    <Header_B>
        <Header_C>
            <Child_A></Child_A>
        </Header_C>
    </Header_B>
</Header_A>
"""

would like to have a list enforced only under Header_C->Child_A as such:

{
  "Header_A": {
    "Child_A": null,
    "Header_B": {
      "Header_C": {
        "Child_A": [null]
      }
    }
  }
}

how to specify the exact path or key-value pair to achieve this?

the following enforces the list to all Child_A:

import json
import xmltodict 

res = xmltodict.parse(xml, force_list={'Child_A'})
print(json.dumps(res, indent=2, sort_keys=True))

{
  "Header_A": {
    "Child_A": [
      null
    ],
    "Header_B": {
      "Header_C": {
        "Child_A": [
          null
        ]
      }
    }
  }
}
jyang
  • 23
  • 4

0 Answers0