vinitkumar / json2xml

json to xml converter in python3
https://json2xml.readthedocs.io/
Other
98 stars 32 forks source link

List items as tags with arguments #146

Open adesso-thomas-lippitsch opened 2 years ago

adesso-thomas-lippitsch commented 2 years ago

Problem

I would like to get the following XML:

<groceries class="foo">
    <item class="bar"/>
    <item class="foobar"/>
    <item class="barfoo"/>
</groceries>

Maybe I am missing something, but with the current implementation I can't achieve this result.

Possible solution

{
    "groceries": {
        "@attrs": {
            "class": "foo"
        },
        "@array": [
            {
                "item": {
                    "@attrs": {
                        "class": "bar"
                    }
                }
            },
            {
                "item": {
                    "@attrs": {
                        "class": "foobar"
                    }
                }
            },
            {
                "item": {
                    "@attrs": {
                        "class": "barfoo"
                    }
                }
            }
        ]
    }
}
vinitkumar commented 2 years ago

If you run this code:

eg.json

{
    "groceries": {
        "@attrs": {
            "class": "foo"
        },
        "@array": [
            {
                "item": {
                    "@attrs": {
                        "class": "bar"
                    }
                }
            },
            {
                "item": {
                    "@attrs": {
                        "class": "foobar"
                    }
                }
            },
            {
                "item": {
                    "@attrs": {
                        "class": "barfoo"
                    }
                }
            }
        ]
    }
}
from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjson

data = readfromjson("/Users/vinitkumar/projects/python/json2xml/examples/eg.json")
print(json2xml.Json2xml(data).to_xml())

We get the following result:

<?xml version="1.0" ?>
<all>
    <groceries class="foo">
        <key>
            <item class="bar"/>
        </key>
        <key>
            <item class="foobar"/>
        </key>
        <key>
            <item class="barfoo"/>
        </key>
    </groceries>
</all>

Seems pretty close to what you want, no?

adesso-thomas-lippitsch commented 2 years ago

Hi, thanks for your reply!

Yes, it is close indeed. But in my case I have to get rid of the "key" tags, which are surrounding each list item. I need it exactly as shown in my example. It is a quite common case in XML, I guess.

I have solved it by writing my own implementation by now, but this adjustment could be an improvement to the package.

Maybe I will create a pull request if I find the time to implement it!

vinitkumar commented 2 years ago

@adesso-thomas-lippitsch Please send a pull request when you find time. I will be more happy to review and merge.

bnabis93 commented 2 years ago

@vinitkumar

I also have the same issue. It was solved by removing the key tag after changing the bytes type of the result to the string type. (Of course, it is not a good solution.)

JSON_PATH = "<YOUR JSON FILE PATH>"
with open(JSON_PATH, "r", encoding="UTF-8") as json_file:
    json_dict = json.load(json_file)

dict_to_xml_data = json2xml.dicttoxml.dicttoxml(
    json_dict, attr_type=False, item_wrap=False, list_headers=True
)

str_converted_data = dict_to_xml_data.decode("utf-8")

str_converted_data = str_converted_data.replace("<key>", "")
str_converted_data = str_converted_data.replace("</key>", "")

bytes_converted_data = bytes(str_converted_data, "utf-8")
print(bytes_converted_data)

By the way, why is the "key" tag is added in the list type of dictionary?

EstherFranssen commented 1 year ago

I have solved it by writing my own implementation by now, but this adjustment could be an improvement to the package.

Maybe I will create a pull request if I find the time to implement it!

Hi,

We are having the same issue. Curious about your implementation. Could you share it? Perhaps make a rough pull request where one of us can refine your implementation suited for the package.

Thanks!

vinitkumar commented 1 year ago

@EstherFranssen @bnabis93 I hear you guys. If possible, please send a PR that fixes this in a backward-compatible way, and I will be more than happy to merge it in the project.

Jeroendevr commented 1 year ago

So this @array thing is working? As it is not documented in the docs 🧐. Something to look for when resolving #163

javadev commented 1 year ago
{
  "groceries": {
    "-class": "foo",
    "item": [
      {
        "-class": "bar",
        "-self-closing": "true"
      },
      {
        "-class": "foobar",
        "-self-closing": "true"
      },
      {
        "-class": "barfoo",
        "-self-closing": "true"
      }
    ]
  },
  "#omit-xml-declaration": "yes"
}

may be converted to

<groceries class="foo">
    <item class="bar"/>
    <item class="foobar"/>
    <item class="barfoo"/>
</groceries>
vinitkumar commented 1 year ago

@javadev @Jeroendevr A new release of json2xml has been released and thanks to the great work of @Jeroendevr, we have list with attributes support live.

Please grab the latest code from https://json2xml.readthedocs.io/en/latest/, https://pypi.org/project/json2xml/3.21.0/ and see if it solves your use case. If there are some bugs, please create a ticket.