Commit d8ff2b9d authored by Priyaranjan DR's avatar Priyaranjan DR

second try

parents
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (JASONANDXML)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/JASONANDXML.iml" filepath="$PROJECT_DIR$/.idea/JASONANDXML.iml" />
</modules>
</component>
</project>
\ No newline at end of file
{"CATALOG": {"CD": [{"TITLE": "dill diya galla", "ARTIST": "Arijit singh", "COUNTRY": "India", "COMPANY": "tseries", "PRICE": "10.90", "YEAR": "2018"}, {"TITLE": "Saiyara", "ARTIST": "Atif Aslam", "COUNTRY": "Uk", "COMPANY": "Records", "PRICE": "9.90", "YEAR": "2015"}, {"TITLE": "Khairiyat", "ARTIST": "Sonu nigam", "COUNTRY": "india", "COMPANY": "radio", "PRICE": "9.90", "YEAR": "2019"}, {"TITLE": "all is well", "ARTIST": "Amir Khan", "COUNTRY": "pak", "COMPANY": "Virgin records", "PRICE": "10.20", "YEAR": "2012"}, {"TITLE": "rockstar", "ARTIST": "kk", "COUNTRY": "india", "COMPANY": "Eros", "PRICE": "9.90", "YEAR": "2014"}]}}
\ No newline at end of file
{
"Name" : "Rani",
"Designation" : "PHP Developer",
"Salary" : 98000,
"Age":27,
"Projects" : [
{"Topic":"Smart Ambulance","Category":"Android Application","Months":2},
{"Topic":"AST","Category":"Embedded System","Months":1},
{"Topic":"Plant Nursery","Category":"Website","Months":3}
]
}
\ No newline at end of file
<Employee><Name>Rani</Name><Designation>PHP Developer</Designation><Salary>98000</Salary><Age>27</Age><Projects><Topic>Smart Ambulance</Topic><Topic>AST</Topic><Topic>Plant Nursery</Topic><Category>Website</Category><Months>3</Months></Projects></Employee>
\ No newline at end of file
import json as j
with open("file.json") as json_format_file:
d = j.load(json_format_file)
import xml.etree.cElementTree as e
r = e.Element("Employee")
e.SubElement(r,"Name").text = d["Name"]
e.SubElement(r,"Designation").text = d["Designation"]
e.SubElement(r,"Salary").text = str(d["Salary"])
e.SubElement(r,"Age").text = str(d["Age"])
project = e.SubElement(r,"Projects")
for z in d["Projects"]:
e.SubElement(project,"Topic").text = z["Topic"]
e.SubElement(project,"Category").text = z["Category"]
e.SubElement(project,"Months").text = str(z["Months"])
a = e.ElementTree(r)
a.write("json_to_xml.xml")
<friendsList>
<friend1>
<name>xyz</name>
</friend1>
<friend2>
<name>abc</name>
</friend2>
<bestfriend>
<name>AAA</name>
</bestfriend>
</friendsList>
This diff is collapsed.
import json
people = '{"name":"priyaranjan","address":"bangalore"}'
data = json.loads(people)
print(data['name'])
print(data['address'])
print(type(data))
\ No newline at end of file
import json
people = '{"name":"priyaranjan","address":"bangalore"}'
data = json.dumps(people)
print(data)
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl'?>
<CATALOG>
<CD>
<TITLE>dill diya galla</TITLE>
<ARTIST>Arijit singh</ARTIST>
<COUNTRY>India</COUNTRY>
<COMPANY>tseries</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>2018</YEAR>
</CD>
<CD>
<TITLE>Saiyara</TITLE>
<ARTIST>Atif Aslam</ARTIST>
<COUNTRY>Uk</COUNTRY>
<COMPANY>Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>2015</YEAR>
</CD>
<CD>
<TITLE>Khairiyat</TITLE>
<ARTIST>Sonu nigam</ARTIST>
<COUNTRY>india</COUNTRY>
<COMPANY>radio</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>2019</YEAR>
</CD>
<CD>
<TITLE>all is well</TITLE>
<ARTIST>Amir Khan</ARTIST>
<COUNTRY>pak</COUNTRY>
<COMPANY>Virgin records</COMPANY>
<PRICE>10.20</PRICE>
<YEAR>2012</YEAR>
</CD>
<CD>
<TITLE>rockstar</TITLE>
<ARTIST>kk</ARTIST>
<COUNTRY>india</COUNTRY>
<COMPANY>Eros</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>2014</YEAR>
</CD>
</CATALOG>
\ No newline at end of file
import json
import xmltodict
with open("xmlfile.xml") as xml_file:
data_dict = xmltodict.parse(xml_file.read())
xml_file.close()
json_data = json.dumps(data_dict)
with open("data.json", "w") as json_file:
json_file.write(json_data)
json_file.close()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment