Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to convert a node of xml to json array #595

Closed
relax-admin opened this issue Mar 16, 2021 · 3 comments
Closed

How to convert a node of xml to json array #595

relax-admin opened this issue Mar 16, 2021 · 3 comments

Comments

@relax-admin
Copy link

relax-admin commented Mar 16, 2021

This is achieved in python, is it possible to add similar parameters in java

In the following example, interfaces are parsed into an array as expected

import json

import xmltodict

t1 = '''
<servers>
  <server>
    <name>host1</name>
    <os>Linux</os>
    <interfaces>
      <interface>
        <name>em0</name>
        <ip_address>10.0.0.1</ip_address>
      </interface>
    </interfaces>
  </server>
</servers>
'''
content = xmltodict.parse(t1, force_list=['servers', 'interfaces'])
data_str = json.dumps(content)
print(data_str)

output

{
  "servers": [
    {
      "server": {
        "name": "host1",
        "os": "Linux",
        "interfaces": [
          {
            "interface": {
              "name": "em0",
              "ip_address": "10.0.0.1"
            }
          }
        ]
      }
    }
  ]
}

is it possible to add similar parameters in java?

public JSONObject parse(String content, List<String> forceList) {
        // to be
        return  org.json.XML.toJSONObject(content);
    }
@stleary
Copy link
Owner

stleary commented Mar 27, 2021

Hi @relax-admin Sorry for the late reply. XML-to-JSON transformations are imperfect. We use XmlParserConfiguration to fine-tune the results. Feel free to post a pull request to implement the desired behavior.

@JanitSri
Copy link
Contributor

I'm going try to tackle this issue @stleary

@stleary
Copy link
Owner

stleary commented Dec 6, 2021

I think this is fixed in #646

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants