From 20fb1f4646771ef2a1915108b388c3a51e08d44a Mon Sep 17 00:00:00 2001 From: Linda Wang <48342363+LindaWang7@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:39:31 -0500 Subject: [PATCH 1/6] created the folder with templated files --- json_to_xml/README.md | 26 ++++++++++++++++++++++++++ json_to_xml/json_to_xml.py | 4 ++++ json_to_xml/requirements.txt | 0 json_to_xml/test-input.json | 0 4 files changed, 30 insertions(+) create mode 100644 json_to_xml/README.md create mode 100644 json_to_xml/json_to_xml.py create mode 100644 json_to_xml/requirements.txt create mode 100644 json_to_xml/test-input.json diff --git a/json_to_xml/README.md b/json_to_xml/README.md new file mode 100644 index 000000000..c831dae3e --- /dev/null +++ b/json_to_xml/README.md @@ -0,0 +1,26 @@ +# Package/Script Name + +Short description of package/script + +- If package, list of functionalities/scripts it can perform +- If standalone script, short description of script explaining what it achieves + +## Setup instructions + +Explain how to setup and run your package/script in user's system + +## Detailed explanation of script, if needed + +If code is not explainable using comments, use this sections to explain your script + +## Output + +Display images/gifs/videos of output/result of your script so that users can visualize it + +## Author(s) + +LindaWang7 + +## Disclaimers, if any + +Use this section to mention if any particular disclaimer is required \ No newline at end of file diff --git a/json_to_xml/json_to_xml.py b/json_to_xml/json_to_xml.py new file mode 100644 index 000000000..fe289949e --- /dev/null +++ b/json_to_xml/json_to_xml.py @@ -0,0 +1,4 @@ +def test(): + print("hi") +if __name__ == "__main__": + test() \ No newline at end of file diff --git a/json_to_xml/requirements.txt b/json_to_xml/requirements.txt new file mode 100644 index 000000000..e69de29bb diff --git a/json_to_xml/test-input.json b/json_to_xml/test-input.json new file mode 100644 index 000000000..e69de29bb From 2d3f1163d893a70ad1eb05e9d24b7ccbd09c9e53 Mon Sep 17 00:00:00 2001 From: Linda Wang <48342363+LindaWang7@users.noreply.github.com> Date: Fri, 22 Nov 2024 22:53:47 -0500 Subject: [PATCH 2/6] implemented the json to xml function --- json_to_xml/json_to_xml.py | 60 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/json_to_xml/json_to_xml.py b/json_to_xml/json_to_xml.py index fe289949e..0f6b43391 100644 --- a/json_to_xml/json_to_xml.py +++ b/json_to_xml/json_to_xml.py @@ -1,4 +1,58 @@ -def test(): - print("hi") +import json +import xml.etree.ElementTree as ET + +def json_to_xml(json_obj, line_padding=""): + """ + Convert a JSON object to an XML string. + """ + result_list = [] + + if isinstance(json_obj, dict): + for key, value in json_obj.items(): + result_list.append(f"{line_padding}<{key}>") + result_list.append(json_to_xml(value, line_padding + " ")) + result_list.append(f"{line_padding}") + elif isinstance(json_obj, list): + for element in json_obj: + result_list.append(json_to_xml(element, line_padding)) + else: + result_list.append(f"{line_padding}{json_obj}") + + return "\n".join(result_list) + + +def save_xml_file(xml_str, output_file): + """ + Save the XML string to a file. + """ + with open(output_file, "w") as file: + file.write(xml_str) + + +def main(): + # Input JSON file + input_json_file = "test-input.json" + # Output XML file + output_xml_file = "test-output.xml" + + try: + # Load JSON data from a file + with open(input_json_file, "r") as json_file: + json_data = json.load(json_file) + + # Convert JSON to XML + xml_data = json_to_xml(json_data) + + # Add XML header + xml_data_with_header = f"\n{xml_data}" + + # Save to XML file + save_xml_file(xml_data_with_header, output_xml_file) + print(f"XML file saved successfully to {output_xml_file}") + + except Exception as e: + print(f"Error: {e}") + + if __name__ == "__main__": - test() \ No newline at end of file + main() From 0fa33c26b9d017eecf7fa230e08a1608b404d263 Mon Sep 17 00:00:00 2001 From: Linda Wang <48342363+LindaWang7@users.noreply.github.com> Date: Fri, 22 Nov 2024 22:54:35 -0500 Subject: [PATCH 3/6] added test case for the json to xml conversion --- json_to_xml/test-input.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/json_to_xml/test-input.json b/json_to_xml/test-input.json index e69de29bb..882c38dfc 100644 --- a/json_to_xml/test-input.json +++ b/json_to_xml/test-input.json @@ -0,0 +1,8 @@ +{ + "note": { + "to": "John", + "from": "Jane", + "heading": "Reminder", + "body": "Don't forget the meeting at 3 PM!" + } +} From 607cbf2ce0884db575a0c3a268960a9bde2a1244 Mon Sep 17 00:00:00 2001 From: Linda Wang <48342363+LindaWang7@users.noreply.github.com> Date: Fri, 22 Nov 2024 22:55:08 -0500 Subject: [PATCH 4/6] added readme with feature description and install instruction --- json_to_xml/README.md | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/json_to_xml/README.md b/json_to_xml/README.md index c831dae3e..9a28da6f0 100644 --- a/json_to_xml/README.md +++ b/json_to_xml/README.md @@ -1,17 +1,30 @@ -# Package/Script Name +# JSON to XML Converter -Short description of package/script +A standalone Python script that converts a JSON file into an XML file. This script is useful for transforming JSON data into a structured XML format for use in systems that require XML-based input. -- If package, list of functionalities/scripts it can perform -- If standalone script, short description of script explaining what it achieves +## Features -## Setup instructions +- Reads JSON data from a file. +- Converts nested JSON objects and arrays into XML elements. +- Outputs well-structured XML files with proper formatting and indentation. +- Adds a standard XML declaration header (``). -Explain how to setup and run your package/script in user's system +## Setup Instructions -## Detailed explanation of script, if needed +### Prerequisites +- Python 3.6 or later installed on your system. -If code is not explainable using comments, use this sections to explain your script +### Installation +1. Clone or download this script to your local system. +2. Save the JSON data you want to convert in a file named `input.json` in the same directory as the script. + +### Running the Script +1. Open a terminal or command prompt. +2. Navigate to the directory containing the script. +3. Run the script using the following command: + ```bash + python json_to_xml.py +The script will generate an XML file named output.xml in the same directory. ## Output @@ -23,4 +36,4 @@ LindaWang7 ## Disclaimers, if any -Use this section to mention if any particular disclaimer is required \ No newline at end of file +N/A \ No newline at end of file From dce1d7a995cbdfc1a0f2ec43c764851d5a8dd0bf Mon Sep 17 00:00:00 2001 From: Linda Wang <48342363+LindaWang7@users.noreply.github.com> Date: Fri, 22 Nov 2024 23:20:09 -0500 Subject: [PATCH 5/6] changed the styling making sure all code is well linted with flake8 --- json_to_xml/json_to_xml.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/json_to_xml/json_to_xml.py b/json_to_xml/json_to_xml.py index 0f6b43391..e309ed8c9 100644 --- a/json_to_xml/json_to_xml.py +++ b/json_to_xml/json_to_xml.py @@ -1,5 +1,5 @@ import json -import xml.etree.ElementTree as ET + def json_to_xml(json_obj, line_padding=""): """ @@ -30,6 +30,9 @@ def save_xml_file(xml_str, output_file): def main(): + """ + Main function to convert a JSON file to an XML file. + """ # Input JSON file input_json_file = "test-input.json" # Output XML file @@ -44,7 +47,9 @@ def main(): xml_data = json_to_xml(json_data) # Add XML header - xml_data_with_header = f"\n{xml_data}" + xml_data_with_header = ( + "\n" + xml_data + ) # Save to XML file save_xml_file(xml_data_with_header, output_xml_file) From 3d918a4c94bdd7f261348b8f55c585f53b13e962 Mon Sep 17 00:00:00 2001 From: Linda Wang <48342363+LindaWang7@users.noreply.github.com> Date: Fri, 22 Nov 2024 23:21:58 -0500 Subject: [PATCH 6/6] added requirements.txt --- json_to_xml/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/json_to_xml/requirements.txt b/json_to_xml/requirements.txt index e69de29bb..c2e111c8b 100644 --- a/json_to_xml/requirements.txt +++ b/json_to_xml/requirements.txt @@ -0,0 +1 @@ +jsonschema==4.19.0 # Optional for JSON schema validation (if needed) \ No newline at end of file