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

List items as tags with arguments #146

Open
adesso-thomas-lippitsch opened this issue Jul 5, 2022 · 9 comments
Open

List items as tags with arguments #146

adesso-thomas-lippitsch opened this issue Jul 5, 2022 · 9 comments

Comments

@adesso-thomas-lippitsch
Copy link

adesso-thomas-lippitsch commented Jul 5, 2022

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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

@bnabis93
Copy link

bnabis93 commented Sep 29, 2022

@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
Copy link

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
Copy link
Owner

@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
Copy link
Contributor

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

@javadev
Copy link

javadev commented Jan 2, 2023

{
  "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
Copy link
Owner

@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.

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

6 participants