Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
converting json to xml
#1
HI,
I am new to python and learning , need help converting json file to xml . I have below code ( found on web ) modified a bit but is not parsing the whole file only few lines: 
appreciate any help! Cry
Thanks!
Output:
Output:
<Core> <case_id> CASE_17BC95 </case_id> <case_id> CASE_17D59E </case_id> </Core>t] Process finished with exit code 0
here is the code:
import json

def json2xml(json_obj, Line_spacing=""):
   result_list = list()
   json_obj_type = type(json_obj)

   if json_obj_type is list:
       for sub_elem in json_obj:
           result_list.append(json2xml(sub_elem, Line_spacing))

       return "\n".join(result_list)
   if json_obj_type is dict:
       for tag_name in json_obj:
           sub_obj = json_obj[tag_name]
           result_list.append("%s<%s>" % (Line_spacing, tag_name))
           result_list.append(json2xml(sub_obj, "\t" + Line_spacing))
           result_list.append("%s</%s>" % (Line_spacing, tag_name))
           # print(result_list)
           return "\n".join(result_list)
   return "%s%s" % (Line_spacing, json_obj)

with open ("C://Users//test//Documents//json2.txt") as s:
data = json.load(s)
print(json2xml(data))
Reply
#2
Python already allows you to convert from JSON into a native dict (using ''json'' or, in versions < 2.6, simplejson), so I found a library that converts native dicts into an XML string.
https://github.com/quandyfactory/dict2xml
It supports int, float, boolean, string (and unicode), array and dict data types and arbitrary nesting (yay recursion).


Another way is that you could open cmd and use pip to install:
json2xml json2xml
(^REQUIRES PIP3 AND PYTHON3)
(Use the following command:)
pip install json2xml json2xml

Then walah':
python from json2xml.json2xml

import Json2xml 
data = Json2xml.fromjsonfile('examples/example.json').data 
data_object = Json2xml(data) data_object.json2xml()
^From a JSON file to XML.
Reply
#3
Thanks for the tips I was able to install with pip but i am not sure where to put/link the python code in my code as you mentioned? what a bout a print statement?
import Json2xml

data = Json2xml.fromjsonfile('examples/example.json').data
data_object = Json2xml(data) data_object.json2xml()


Thanks!

I Imported the json2xml and now all of json2xml saying re declared json2xml above with no usage? can you update my code with what you are mentioning? so I can run it here and see?
Thanks!
Reply
#4
(Mar-10-2017, 07:53 PM)pasi12 Wrote: Thanks for the tips I was able to install with pip but i am not sure where to put/link the python code in my code as you mentioned? what a bout a print statement?
import Json2xml

data = Json2xml.fromjsonfile('examples/example.json').data
data_object = Json2xml(data) data_object.json2xml()


Thanks!

I Imported the json2xml and now all of json2xml saying re declared json2xml above with no usage? can you update my code with what you are mentioning? so I can run it here and see?
Thanks!
Please completely scrap what I said before. PIP does not allow you to use from src
You need to use PIP3

So do thi' following:
pip3 install json2xml
^Voodoo magic A (Installs json2xml via PIP3)

python -m src.cli --file="examples/example.json"
^Voodoo magic B (Allows you to use json2xml via cmd)          OR:

from src.json2xml import Json2xml
data = Json2xml.fromjsonfile('examples/example.json').data
data_object = Json2xml(data)
data_object.json2xml() #xml output
^Voodoo magic C (json2xml via a script)
Do not try to implement this into your current script as it probally will not work. I cant see it working anyway.


MAKE A NEW SCRIPT USING THAT CODE REPLACING 'examples/example.json' with the location of your .json file.

Enjoy if this works. Good luck!
Reply
#5
Your json2xml function has wrong indentation on line 19 - return is inside for loop, so processing ends after parsing first member in dictionary. With that corrected it might work reasonably well.

import json

def json2xml(json_obj, Line_spacing=""):
   result_list = list()
   json_obj_type = type(json_obj)
 
   if json_obj_type is list:
       for sub_elem in json_obj:
           result_list.append(json2xml(sub_elem, Line_spacing))
 
       return "\n".join(result_list)
   if json_obj_type is dict:
       for tag_name in json_obj:
           sub_obj = json_obj[tag_name]
           result_list.append("%s<%s>" % (Line_spacing, tag_name))
           result_list.append(json2xml(sub_obj, "\t" + Line_spacing))
           result_list.append("%s</%s>" % (Line_spacing, tag_name))
           # print(result_list)
       return "\n".join(result_list)      # <-- this one was misindented
   return "%s%s" % (Line_spacing, json_obj)
Reply
#6
(Mar-10-2017, 09:36 PM)zivoni Wrote: Your json2xml function has wrong indentation on line 19 - return is inside for loop, so processing ends after parsing first member in dictionary. With that corrected it might work reasonably well.

import json


def json2xml(json_obj, Line_spacing=""):
   result_list = list()
   json_obj_type = type(json_obj)
 
   if json_obj_type is list:
       for sub_elem in json_obj:
           result_list.append(json2xml(sub_elem, Line_spacing))
 
       return "\n".join(result_list)
   if json_obj_type is dict:
       for tag_name in json_obj:
           sub_obj = json_obj[tag_name]
           result_list.append("%s<%s>" % (Line_spacing, tag_name))
           result_list.append(json2xml(sub_obj, "\t" + Line_spacing))
           result_list.append("%s</%s>" % (Line_spacing, tag_name))
           # print(result_list)
       return "\n".join(result_list)      # <-- this one was misindented
   return "%s%s" % (Line_spacing, json_obj)

Oh my, I just reread his code an realised he was using json2xml. I thought he was just using the json library and didnt finish reading it. Sorry about that. I feel stupid :P

EDIT: Although that would work, it is very extensive compared to other methods.
Reply
#7
ok.. I already installed the pip when I try to install pip3 its says you already installed all the files? how do I uninstall pip pkg?

Dudei.. its working now!! thanks so much the proble was as you mentioned the misindented

return "\n".join(result_list) # <-- this one was misindented

Thanks Again! I cant beleive the indentation will cause all these problems! don't like Python! :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,366 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  Converting cells in excel to JSON format desmondtay 4 1,721 May-23-2022, 10:31 AM
Last Post: Larz60+
  Trouble converting JSON String to Dictionary RBeck22 7 5,099 Mar-28-2019, 12:12 PM
Last Post: RBeck22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020