vinitkumar/json2xml

json to xml conversion with xml namespace

althaf004 opened this issue · 3 comments

Hi, i need to convert a json data to xml. the sample code is as below.


from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjson
data = readfromstring(

 '{"ra:covered-product-agreement":{"login":"mojombo","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}}'
)
return json2xml.Json2xml(data, attr_type=False).to_xml()

where "ra" is the namespace. but the conversion is not exactly happening.

the result expecting is

<ra:covered-product-agreement>
		<login>mojombo</login>
		<id>1</id>
		<avatar_url>https://avatars0.githubusercontent.com/u/1?v=4</avatar_url>
	</ra:covered-product-agreement>

with current version the result received is

<key name="ra:covered-product-agreement">
		<login>mojombo</login>
		<id>1</id>
		<avatar_url>https://avatars0.githubusercontent.com/u/1?v=4</avatar_url>
	</key>

Please suggest a way to achieve this.

Thanks

Just saw this ticket, let me look into this and let you know.

hey @vinitkumar , got a work around for this don`t if its the correct way but still it worked


CREATE OR REPLACE FUNCTION public.textxmldict()
 RETURNS text
 LANGUAGE plpython3u
AS $function$
import xmltodict
from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjson
import json

sample_json = plpy.execute("select content from items where id=1"); 
data = readfromstring(sample_json[0]["content"])
json_to_xml = xmltodict.unparse(data)

return str(json_to_xml)
$function$

@althaf004 The functionality is implemented in the latest release. Please check