JSON Conversion
Citizen Timeline is based on JSON, some applications cannot handle JSON directly (e.g. some Large Language Model software, so we are in the process of developing a convertor between JSON and English.
Sample Input
json_data = '''
{
"name": "John Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA"
},
"phone_numbers": [
"123-456-7890",
"987-654-3210"
],
"email": "john.doe@example.com"
}
'''
Without Numbering
import json
def json_to_text(json_obj, indent=0):
text = ""
indent_text = " " * indent
for key, value in json_obj.items():
if isinstance(value, dict):
text += f"{indent_text}{key.capitalize()} contains:\n"
text += json_to_text(value, indent + 1)
elif isinstance(value, list):
text += f"{indent_text}{key.capitalize()} are:\n"
for item in value:
if isinstance(item, dict):
text += json_to_text(item, indent + 1)
else:
text += f"{indent_text} {item}\n"
else:
text += f"{indent_text}{key.capitalize()} is {value}\n"
return text
Sample Output
Name is John Doe
Age is 30
Address contains:
Street is 123 Main St
City is Anytown
State is CA
Phone_numbers are:
123-456-7890
987-654-3210
Email is john.doe@example.com
With Numbering
import json
def json_to_text(json_obj, indent=0, prefix=""):
text = ""
indent_text = " " * indent
counter = 1
for key, value in json_obj.items():
current_prefix = f"{prefix}{counter}."
if isinstance(value, dict):
text += f"{indent_text}{current_prefix} {key.capitalize()} contains:\n"
text += json_to_text(value, indent + 1, f"{current_prefix}.")
elif isinstance(value, list):
text += f"{indent_text}{current_prefix} {key.capitalize()} are:\n"
for i, item in enumerate(value, 1):
item_prefix = f"{current_prefix}{i}."
if isinstance(item, dict):
text += json_to_text(item, indent + 1, item_prefix)
else:
text += f"{indent_text} {item_prefix} {item}\n"
else:
text += f"{indent_text}{current_prefix} {key.capitalize()} is {value}\n"
counter += 1
return text
Sample Output
1. Name is John Doe
2. Age is 30
3. Address contains:
3.1. Street is 123 Main St
3.2. City is Anytown
3.3. State is CA
4. Phone_numbers are:
4.1. 123-456-7890
4.2. 987-654-3210
5. Email is john.doe@example.com