Import the necessary modules from FormatFusion
from FormatFusion.converter import Converter
Convert YAML to JSON
from FormatFusion.converter import Converter
yaml_data = """
name: John Doe
age: 30
city: New York
"""
json_data = Converter().from_yaml(yaml_data).to_json()
print(json_data)
The code above will returned
{
"age": 30,
"city": "New York",
"name": "John Doe"
}
Convert JSON to YAML
from FormatFusion.converter import Converter
json_data = """{
"name": "John Doe",
"age": 30,
"city": "New York"
}"""
yaml_data = Converter().from_json(json_data).to_yaml()
print(yaml_data)
The code above will returned
age: 30
city: New York
name: John Doe
Convert YAML to XML
from FormatFusion.converter import Converter
yaml_data = """
name: John Doe
age: 30
address:
street: Jalan Merdeka
city: Jakarta
province: DKI Jakarta
"""
xml = Converter().from_yaml(xml).to_xml()
print(xml)
The code above will returned
<?xml version="1.0" ?>
<root>
<name>John Doe</name>
<age>30</age>
<address>
<street>Jalan Merdeka</street>
<city>Jakarta</city>
<province>DKI Jakarta</province>
</address>
</root>
Convert JSON to XML
from FormatFusion.converter import Converter
json_data = """{
"name": "John Doe",
"age": 30,
"address": {
"street": "Jalan Merdeka",
"city": "Jakarta",
"province": "DKI Jakarta"
}
}"""
xml = Converter().from_json(xml).to_xml()
print(xml)
The code above will returned
<?xml version="1.0" ?>
<root>
<name>John Doe</name>
<age>30</age>
<address>
<street>Jalan Merdeka</street>
<city>Jakarta</city>
<province>DKI Jakarta</province>
</address>
</root>
Convert XML to YAML
from FormatFusion.converter import Converter
xml_data = """
<?xml version="1.0" ?>
<user>
<name>John Doe</name>
<age>30</age>
<address>
<street>Jalan Merdeka</street>
<city>Jakarta</city>
<province>DKI Jakarta</province>
</address>
</user>
"""
yaml_data = Converter().from_xml(xml_data).to_yaml()
print(yaml_data)
The code above will returned
user:
address:
city: Jakarta
province: DKI Jakarta
street: Jalan Merdeka
age: "30"
name: John Doe
Convert XML to JSON
from FormatFusion.converter import Converter
xml_data = """
<?xml version="1.0" ?>
<user>
<name>John Doe</name>
<age>30</age>
<address>
<street>Jalan Merdeka</street>
<city>Jakarta</city>
<province>DKI Jakarta</province>
</address>
</user>
"""
json_data = Converter().from_xml(xml_data).to_json()
print(json_data)
The code above will returned
{
"user": {
"address": {
"city": "Jakarta",
"province": "DKI Jakarta",
"street": "Jalan Merdeka"
},
"age": "30",
"name": "John Doe"
}
}
The io
module provides functions for reading data from file or url and save data in expected format
This function is used to read data from file. Here is the example of using read_file
function:
from FormatFusion.io import IO
data = IO().read_file('data.yaml')
print(data)
This function is used to scan URL content, it will return a data with any format, based on its original format. Here is the example how to use scan_url
function:
from FormatFusion.io import IO
yaml_data = IO().scan_url(
"https://github.com/aliftech/FormatFusion/raw/master/test/test.yaml")
print(yaml_data)
This function is used to save data into a specific file with specified format (txt, md, html, yml, yaml, json). Here is the example of using save_file
function:
from FormatFusion.io import IO
yaml_data = IO().scan_url(
"https://github.com/aliftech/FormatFusion/raw/master/test/test.yaml")
IO().save_file('data.yaml', yaml_data)
This function is used to convert yaml, json, and xml data to csv or vice versa.
Here is an example how you can use this functionality:
This function is used to convert yaml data to csv format. Here is the example of using this function.
from FormatFusion.yaml_to_csv import yaml_to_csv
yaml_data = """
name: John Doe
age: 30
address:
street: Jalan Merdeka
city: Jakarta
province: DKI Jakarta
"""
csv_data = yaml_to_csv(yaml_data)
print(csv_data)
The code above will returned
name,age,address
John Doe,30,"{'street': 'Jalan Merdeka', 'city': 'Jakarta', 'province': 'DKI Jakarta'}"
This function is used to convert json data into csv format.
from FormatFusion.json_to_csv import json_to_csv
json_data = {
"name": "John Doe",
"age": 30,
"city": "New York"
}
csv_data = json_to_csv(json_data)
print(csv_data)
The code above will returned
name,age,city
John Doe,30,New York
This function is used to convert xml data into csv format.
from FormatFusion.xml_to_csv import xml_to_csv
xml_data = """
<?xml version="1.0" ?>
<user>
<item>
<name>John Doe</name>
<age>30</age>
<address>
<street>Jalan Merdeka</street>
<city>Jakarta</city>
<province>DKI Jakarta</province>
</address>
</item>
</user>
"""
csv_data = xml_to_csv(xml_data, "user", "item")
print(csv_data)
The code above will returned
name,age,address
John Doe,30,"{'street': 'Jalan Merdeka', 'city': 'Jakarta', 'province': 'DKI Jakarta'}"
Here is the explanation of the code above:
- in the
csv_data = xml_to_csv(xml_data, "user", "item")
- the xml_data is the data that will be converted to cvs data. - "user" - this is the tag name that will be used as a targeted data.
- "item" - this is the child tag name inside "user" which contains individual user information.
This function is used to convert csv data into yaml format.
from FormatFusion.csv_to_yaml import csv_to_yaml
csv_data = """name,age,city
John Doe,30,New York
"""
yaml_data = csv_to_yaml(csv_data)
print(yaml_data)
The code above will returned
age: "30"
city: New York
name: John Doe
This function is used to convert csv data into json format.
from FormatFusion.csv_to_json import csv_to_json
csv_data = """name,age,city
John Doe,30,New York
Alice,25,Jakarta
"""
json_data = csv_to_json(csv_data)
print(json_data)
The code above will returned
[
{ "name": "John Doe", "age": "30", "city": "New York" },
{ "name": "Alice", "age": "25", "city": "Jakarta" }
]
This function is used to convert csv data into xml format.
from FormatFusion.csv_to_xml import csv_to_xml
csv_data = """name,age,city
John Doe,30,New York
Alice,25,Jakarta
"""
xml_data = csv_to_xml(csv_data)
print(xml_data)
The code above will returned
<?xml version="1.0" ?>
<root>
<item>
<name>John Doe</name>
<age>30</age>
<city>New York</city>
</item>
<item>
<name>Alice</name>
<age>25</age>
<city>Jakarta</city>
</item>
</root>