vinitkumar/json2xml

validated json fails conversion

Closed this issue · 4 comments

Ran json code through online validator, came out valid. Ran through json2xml and failed.
online validator:
https://jsonlint.com/
error message:
"python3 j2x.py
Traceback (most recent call last):
File "j2x.py", line 9, in
xmltodict.unparse(python_dict,output=xml_file)
File "/home/jim/.local/lib/python3.8/site-packages/xmltodict.py", line 494, in unparse
raise ValueError('Document must have exactly one root.')
ValueError: Document must have exactly one root."

json code:
{ "adult": false, "backdrop_path": "/hZkgoQYus5vegHoetLkCJzb17zJ.jpg", "belongs_to_collection": null, "budget": 63000000, "genres": [ { "id": 18, "name": "Drama" }, { "id": 53, "name": "Thriller" }, { "id": 35, "name": "Comedy" } ], "homepage": "http://www.foxmovies.com/movies/fight-club", "id": 550, "imdb_id": "tt0137523", "original_language": "en", "original_title": "Fight Club", "overview": "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.", "popularity": 52.046, "poster_path": "/pB8BM7pdSp6B6Ih7QZ4DrQ3PmJK.jpg", "production_companies": [ { "id": 508, "logo_path": "/7cxRWzi4LsVm4Utfpr1hfARNurT.png", "name": "Regency Enterprises", "origin_country": "US" }, { "id": 711, "logo_path": "/tEiIH5QesdheJmDAqQwvtN60727.png", "name": "Fox 2000 Pictures", "origin_country": "US" }, { "id": 20555, "logo_path": "/hD8yEGUBlHOcfHYbujp71vD8gZp.png", "name": "Taurus Film", "origin_country": "DE" }, { "id": 54051, "logo_path": null, "name": "Atman Entertainment", "origin_country": "" }, { "id": 54052, "logo_path": null, "name": "Knickerbocker Films", "origin_country": "US" }, { "id": 4700, "logo_path": "/A32wmjrs9Psf4zw0uaixF0GXfxq.png", "name": "The Linson Company", "origin_country": "US" }, { "id": 25, "logo_path": "/qZCc1lty5FzX30aOCVRBLzaVmcp.png", "name": "20th Century Fox", "origin_country": "US" } ], "production_countries": [ { "iso_3166_1": "US", "name": "United States of America" } ], "release_date": "1999-10-15", "revenue": 100853753, "runtime": 139, "spoken_languages": [ { "english_name": "English", "iso_639_1": "en", "name": "English" } ], "status": "Released", "tagline": "Mischief. Mayhem. Soap.", "title": "Fight Club", "video": false, "vote_average": 8.434, "vote_count": 26534 }

Hi @jimlynnjulian

I just pasted your JSON into Jsonlint.com to get a better formatted JSON

{
	"adult": false,
	"backdrop_path": "/hZkgoQYus5vegHoetLkCJzb17zJ.jpg",
	"belongs_to_collection": null,
	"budget": 63000000,
	"genres": [{
		"id": 18,
		"name": "Drama"
	}, {
		"id": 53,
		"name": "Thriller"
	}, {
		"id": 35,
		"name": "Comedy"
	}],
	"homepage": "http://www.foxmovies.com/movies/fight-club",
	"id": 550,
	"imdb_id": "tt0137523",
	"original_language": "en",
	"original_title": "Fight Club",
	"overview": "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.",
	"popularity": 52.046,
	"poster_path": "/pB8BM7pdSp6B6Ih7QZ4DrQ3PmJK.jpg",
	"production_companies": [{
		"id": 508,
		"logo_path": "/7cxRWzi4LsVm4Utfpr1hfARNurT.png",
		"name": "Regency Enterprises",
		"origin_country": "US"
	}, {
		"id": 711,
		"logo_path": "/tEiIH5QesdheJmDAqQwvtN60727.png",
		"name": "Fox 2000 Pictures",
		"origin_country": "US"
	}, {
		"id": 20555,
		"logo_path": "/hD8yEGUBlHOcfHYbujp71vD8gZp.png",
		"name": "Taurus Film",
		"origin_country": "DE"
	}, {
		"id": 54051,
		"logo_path": null,
		"name": "Atman Entertainment",
		"origin_country": ""
	}, {
		"id": 54052,
		"logo_path": null,
		"name": "Knickerbocker Films",
		"origin_country": "US"
	}, {
		"id": 4700,
		"logo_path": "/A32wmjrs9Psf4zw0uaixF0GXfxq.png",
		"name": "The Linson Company",
		"origin_country": "US"
	}, {
		"id": 25,
		"logo_path": "/qZCc1lty5FzX30aOCVRBLzaVmcp.png",
		"name": "20th Century Fox",
		"origin_country": "US"
	}],
	"production_countries": [{
		"iso_3166_1": "US",
		"name": "United States of America"
	}],
	"release_date": "1999-10-15",
	"revenue": 100853753,
	"runtime": 139,
	"spoken_languages": [{
		"english_name": "English",
		"iso_639_1": "en",
		"name": "English"
	}],
	"status": "Released",
	"tagline": "Mischief. Mayhem. Soap.",
	"title": "Fight Club",
	"video": false,
	"vote_average": 8.434,
	"vote_count": 26534
}

Now, with this code:

I saved this JSON file as t.json and ran this python code:

from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjson

# get the data from an URL
data = readfromjson("t.json")
print(json2xml.Json2xml(data).to_xml())

Which gives me this:

<?xml version="1.0" ?>
<all>
	<adult type="bool">false</adult>
	<backdrop_path type="str">/hZkgoQYus5vegHoetLkCJzb17zJ.jpg</backdrop_path>
	<belongs_to_collection type="null"/>
	<budget type="int">63000000</budget>
	<genres type="list">
		<item type="dict">
			<id type="int">18</id>
			<name type="str">Drama</name>
		</item>
		<item type="dict">
			<id type="int">53</id>
			<name type="str">Thriller</name>
		</item>
		<item type="dict">
			<id type="int">35</id>
			<name type="str">Comedy</name>
		</item>
	</genres>
	<homepage type="str">http://www.foxmovies.com/movies/fight-club</homepage>
	<id type="int">550</id>
	<imdb_id type="str">tt0137523</imdb_id>
	<original_language type="str">en</original_language>
	<original_title type="str">Fight Club</original_title>
	<overview type="str">A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground &quot;fight clubs&quot; forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.</overview>
	<popularity type="float">52.046</popularity>
	<poster_path type="str">/pB8BM7pdSp6B6Ih7QZ4DrQ3PmJK.jpg</poster_path>
	<production_companies type="list">
		<item type="dict">
			<id type="int">508</id>
			<logo_path type="str">/7cxRWzi4LsVm4Utfpr1hfARNurT.png</logo_path>
			<name type="str">Regency Enterprises</name>
			<origin_country type="str">US</origin_country>
		</item>
		<item type="dict">
			<id type="int">711</id>
			<logo_path type="str">/tEiIH5QesdheJmDAqQwvtN60727.png</logo_path>
			<name type="str">Fox 2000 Pictures</name>
			<origin_country type="str">US</origin_country>
		</item>
		<item type="dict">
			<id type="int">20555</id>
			<logo_path type="str">/hD8yEGUBlHOcfHYbujp71vD8gZp.png</logo_path>
			<name type="str">Taurus Film</name>
			<origin_country type="str">DE</origin_country>
		</item>
		<item type="dict">
			<id type="int">54051</id>
			<logo_path type="null"/>
			<name type="str">Atman Entertainment</name>
			<origin_country type="str"/>
		</item>
		<item type="dict">
			<id type="int">54052</id>
			<logo_path type="null"/>
			<name type="str">Knickerbocker Films</name>
			<origin_country type="str">US</origin_country>
		</item>
		<item type="dict">
			<id type="int">4700</id>
			<logo_path type="str">/A32wmjrs9Psf4zw0uaixF0GXfxq.png</logo_path>
			<name type="str">The Linson Company</name>
			<origin_country type="str">US</origin_country>
		</item>
		<item type="dict">
			<id type="int">25</id>
			<logo_path type="str">/qZCc1lty5FzX30aOCVRBLzaVmcp.png</logo_path>
			<name type="str">20th Century Fox</name>
			<origin_country type="str">US</origin_country>
		</item>
	</production_companies>
	<production_countries type="list">
		<item type="dict">
			<iso_3166_1 type="str">US</iso_3166_1>
			<name type="str">United States of America</name>
		</item>
	</production_countries>
	<release_date type="str">1999-10-15</release_date>
	<revenue type="int">100853753</revenue>
	<runtime type="int">139</runtime>
	<spoken_languages type="list">
		<item type="dict">
			<english_name type="str">English</english_name>
			<iso_639_1 type="str">en</iso_639_1>
			<name type="str">English</name>
		</item>
	</spoken_languages>
	<status type="str">Released</status>
	<tagline type="str">Mischief. Mayhem. Soap.</tagline>
	<title type="str">Fight Club</title>
	<video type="bool">false</video>
	<vote_average type="float">8.434</vote_average>
	<vote_count type="int">26534</vote_count>
</all>

So it just works for me, can you please check your code and confirm if the bug is not in your code?

Hello,

Sorry, I'm covered up in work. Besides that, my computer went weird on me and I ended up making a fresh install of Windows.
I'll post my results when I can get around to it. I'm sure you're probably right. Have a good one.

The problem was my code. I installed using pip3. My interpretation of the importation scheme was wrong. Just now, I examined the tests in the distribution and saw my mistake. Thanks for the feedback.

The problem was my code. I installed using pip3. My interpretation of the importation scheme was wrong. Just now, I examined the tests in the distribution and saw my mistake. Thanks for the feedback.

No problem. It's good that you know where the problem was. In future, if you have any issues with the library, feel free to make a ticket and I will be happy to help in all possible ways. Have a good day ahead. 👍🏼