Skip to content

Instantly share code, notes, and snippets.

@Amir-P
Last active October 2, 2023 09:41
Show Gist options
  • Save Amir-P/d7017ce65da1bd40f4372dfbd2b68ec2 to your computer and use it in GitHub Desktop.
Save Amir-P/d7017ce65da1bd40f4372dfbd2b68ec2 to your computer and use it in GitHub Desktop.
Convert JSON exported contacts from Telegram to vCard (.vcf)
import sys
import json
input_file = sys.argv[1]
output_file = sys.argv[2]
output_file = open(output_file, 'w')
def to_vcf_string(first_name, last_name, phone):
vcf_lines = []
vcf_lines.append('BEGIN:VCARD')
vcf_lines.append('VERSION:4.0')
vcf_lines.append('N:%s' % (last_name + ';' + first_name))
vcf_lines.append('FN:%s' % (first_name + ('' if len(last_name) == 0 else ' ') + last_name))
vcf_lines.append('TEL:%s' % phone)
vcf_lines.append('END:VCARD')
vcf_string = '\n'.join(vcf_lines) + '\n'
return vcf_string
with open(input_file) as json_file:
data = json.load(json_file)
contacts_list = data['contacts']['list']
for contact in contacts_list:
first_name = contact['first_name']
last_name = contact['last_name']
if len(first_name) == 0 and len(last_name) == 0:
continue
phone = contact['phone_number']
output_file.write(to_vcf_string(first_name, last_name, phone).encode('utf-8'))
output_file.close()
@Amir-P
Copy link
Author

Amir-P commented Aug 30, 2020

Usage: python converter.py input_file.json output_file.vcf

@SHBnik
Copy link

SHBnik commented Apr 8, 2021

Hi,
Thanks for this helpful code.
I have a suggestion.
replace line 32 with :
output_file.write(vcf_writer(first_name, last_name, phone).encode('utf-8'))
adding the .encode('utf-8') will convert the arabic and the persian contacts.

@Amir-P
Copy link
Author

Amir-P commented Apr 8, 2021

Thanks @SHBnik for the suggestion.

@cmonty14
Copy link

I get this error running converter.py:

$ python converter.py Downloads/contacts-2023-02-05.json contacts.vcf
Traceback (most recent call last):
  File "/home/thomas/converter.py", line 22, in <module>
    contacts_list = data['contacts']['list']
TypeError: list indices must be integers or slices, not str

@knightynite
Copy link

I get this error running converter.py:

$ python converter.py Downloads/contacts-2023-02-05.json contacts.vcf
Traceback (most recent call last):
  File "/home/thomas/converter.py", line 22, in <module>
    contacts_list = data['contacts']['list']
TypeError: list indices must be integers or slices, not str

try this:

import sys
import json

def to_vcf_string(first_name, last_name, phone):
vcf_lines = [
'BEGIN:VCARD',
'VERSION:4.0',
f'N:{last_name};{first_name}',
f'FN:{first_name}{" " if last_name else ""}{last_name}',
f'TEL:{phone}',
'END:VCARD'
]
return '\n'.join(vcf_lines) + '\n'

def main(input_file_path, output_file_path):
with open(input_file_path) as json_file:
data = json.load(json_file)
contacts_list = data['contacts']['list']

    with open(output_file_path, 'w') as output_file:
        for contact in contacts_list:
            first_name = contact.get('first_name', '')
            last_name = contact.get('last_name', '')
            
            if not first_name and not last_name:
                continue
            
            phone = contact.get('phone_number', '')
            output_file.write(to_vcf_string(first_name, last_name, phone))

if name == "main":
if len(sys.argv) < 3:
print("Usage: script_name input_file output_file")
sys.exit(1)

main(sys.argv[1], sys.argv[2])

@MrWesturn
Copy link

Hello
how give him json file?

@Amir-P
Copy link
Author

Amir-P commented Oct 2, 2023

Hi. Just run the script passing json file path and desired output path as input like this: python converter.py json_file_path output_file_path. @MrWesturn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment