converters¶
The converters module
holds multiple converters for strings to objects.
- csv_to_json(data: str) list ¶
Converts a CSV string to a list of json dicts. Will use the first row as the keys for all other rows.
- Parameters:
data – The
str
to be converted.- Returns:
A
list
of the rows asdict
items.
- json_to_csv(data: List[dict]) str ¶
Converts a list of dictionaries to a CSV string. Will check every item to determine needed headers.
- Parameters:
data – The
list
to convert.- Returns:
A
str
of the items indata
converted to CSV.
- json_to_xml(data: dict, favor_attributes: bool = False, avoid_text_and_elements: bool = True)¶
Converts JSON data to an XML representation. If
favor_attributes
isTrue
then:Should a value be a dictionary, it will be interpreted as a contained element.
Should a value be a list, it will be interpreted as a series of XML elements with the same tag.
If a value is not a dictionary or a list, it will be stored under the current element as an attribute labeled with its key.
If
favor_attributes
isFalse
:Should a value be a dictionary, it will be interpreted as a contained element.
Should a value be a list, it will be interpreted as a series of XML elements with the same tag.
If a value is not a dictionary or a list, it will be stored as the text of an element with its key as a tag.
If ``
As long as
avoid_text_and_elements
isTrue
, if the key'text'
is present, behavior will be overriden, and the value under'text'
will be used as the text of an element, while any other pairs will be entered as attributes. A non-dictionary value in a list will also be interpreted as the text of a containing element.In the case that
avoid_text_and_elements
isFalse
, any other pairs will be interpreted as elements, so long asfavor_attributes
is notTrue
.Example:
data = { 'a': { 'b': { 'c': {'text': '', 'd': 'e', 'f': 'g'}, 'h': 'i' }, 'j': { 'k': { 'l': [ {'m': 'o', 'n': 'r'}, {'m': 'p', 'n': 's', 'text': t}, [{'m': 'q'}, 'u'], {'m': 'v', 'text': 'w'} ], 'x': { 'y': ['ab', 'ac'] } } } } } xml = json_to_xml(data) # xml == ('<a>' # '<b>' # '<c d="e" f="g" />' # '<h>i</h>' # '</b>' # '<j>' # '<k>' # '<l>' # '<m>o</m>' # '<n>r</n>' # '</l>' # '<l m="p" n="s">t</l>' # '<l m="q">u</l>' # '<l m="v">w</l>' # '<x>' # '<y>ab</y>' # '<y>ac</y>' # '</x>' # '</k>' # '</j>' # '</a>') xml_chaos = json_to_xml(data, avoid_text_and_elements=False) # xml_chaos == ('<a>' # '<b>' # '<c>' # '<d>e</d>' # '<f>g</f>' # '</c>' # '<h>i</h>' # '</b>' # '<j>' # '<k>' # '<l>' # '<m>o</m>' # '<n>r</n>' # '</l>' # '<l>t' # '<m>p</m>' # '<n>s</n>' # '</l>' # '<l>u' # '<m>q</m> # '</l>' # '<l>w' # '<m>v</m>' # '</l>' # '<x>' # '<y>ab</y>' # '<y>ac</y>' # '</x>' # '</k>' # '</j>' # '</a>') xml_fa = json_to_xml(data, True) # xml_fa == ('<a>' # '<b h="i">' # '<c d="e" f="g" />' # '</b>' # '<j>' # '<k>' # '<l m="o" n="r" />' # '<l m="p" n="s">t</l>' # '<l m="q">u</l>' # '<l m="v">w</l>' # '<x>' # '<y>ab</y>' # '<y>ac</y>' # '</x>' # '</k>' # '</j>' # '</a>')
- Parameters:
data (dict) – The JSON data to convert.
favor_attributes (bool) – Whether to favor storing values as attributes or not.
avoid_text_and_elements (bool) – If
True
, then attempts will be made to avoid an element having both text and internal elements. IfFalse
, no such attempts will be made.
- Returns:
A string representing the JSON object converted to an XML format.
- jsonl_to_json(data: str) list ¶
Converts a JSONL string to a list of objects.
- Parameters:
data – The JSONL string to convert.
- Returns:
A
list
containing thedict
andlist
items stored in the JSONL string.
- list_to_string_list(data: list) str ¶
Generates a string representation of a list in proper sentence form. Uses the Oxford comma.
- Parameters:
data (list) – The list to convert.
- Returns:
A string containing the list converted to proper sentence form.
- wonky_json_to_json(data: str, different_quote: str = "'")¶
Converts a JSON-like string that uses a non-standard quote character into valid JSON. At least, it tries to.
- Parameters:
data – The JSON-like string to be converted to a
dict
orlist
.different_quote – The different quote that was used for the string.
- Returns:
The string converted to a
dict
orlist
, depending on what was encoded.
- xml_to_json(data: str, sans_attributes: bool = False)¶
Converts XML data to a JSON representation. Attributes will be interpreted as keys of a dict, as will tags within elements. If there are multiple of a tag within one element, the values inside of those tags will be treated as individual items and added to a list under that tag as a key. Genuine text values of elements will be included in dicts under the key
'text'
. If sans_attributes isTrue
, then attributes will not be considered as keys unless there are no internal elements and no text, in which case they will be included.Example:
data = '''<a> <b> <c d="e" f="g"/> <h>i</h> </b> <j> <k> <l m="o" n="r"/> <l m="p" n="s">t</l> <l m="q">u</l> <l m="v">w</l> <x z="aa"> <y>ab</y> <y>ac</y> </x> </k> </j> </a>''' xml = xml_to_json(data) # xml == { # 'a': { # 'b': { # 'c': {'d': 'e', 'f': 'g', 'text': None}, # 'h': {'text': 'i'}, # 'text': str # }, # 'j': { # 'k': { # 'l': [ # {'m': 'o', 'n': 'r', 'text': None}, # {'m': 'p', 'n': 's', 'text': 't'}, # {'m': 'q', 'text': 'u'}, # {'m': 'v', 'text': 'w'} # ], # 'x': { # 'y': [ # {'text': 'ab'}, # {'text': 'ac'} # ], # 'z': 'aa', # 'text': str # }, # 'text': str # }, # 'text': str # }, # 'text': str # } # } xml_sa = xml_to_json(data, True) # xml_sa == { # 'a': { # 'b': { # 'c': {'d': 'e', 'f': 'g'}, # 'h': 'i' # }, # 'j': { # 'k': { # 'l': [{'m': 'o', 'n':'r'}, 't', 'u', 'w'], # 'x': { # 'y': ['ab', 'ac'] # } # } # } # } # }
- Parameters:
data – The XML data to parse.
sans_attributes – Whether to exclude attributes from the JSON output.
- Returns:
The JSON representation of the XML data.