Chris Chris - 12 days ago 5
JSON Question

How to create a json array from an icalendar file utilizing python?

I have an ical file that I'd like to read in a json array format utilizing python. I've found that there is a package called icalendar 3.11, but it doesn't translate the iCalendar Text to a json array format.

See below for icalendar text:

BEGIN:VCALENDAR
PRODID;X-RICAL-TZSOURCE=TZINFO:-//Airbnb Inc//Hosting Calendar 0.8.8//EN
CALSCALE:GREGORIAN
VERSION:2.0
BEGIN:VEVENT
DTEND;VALUE=DATE:20161111
DTSTART;VALUE=DATE:20161110
UID:-4sxkntmd7ehk-4ybn124l9iwr@bnbhome.com
DESCRIPTION:CHECKIN: 11/10/2016\nCHECKOUT: 11/11/2016\nNIGHTS: 1\nPHONE:
+1 (111) 111-1111\nEMAIL: gregory-66i5v1thmkdva1u4@test.bnb.com\nPRO
PERTY: Rittenhouse Square Cozy Studio Apartment\n
SUMMARY:John Smith (XXXXXX)
LOCATION:Rittenhouse Square Cozy Studio Apartment
END:VEVENT
BEGIN:VEVENT
DTEND;VALUE=DATE:20170104
DTSTART;VALUE=DATE:20170102
UID:-4sxkntmd7ehk-wps9gpnpzxkj@bnbhome.com
DESCRIPTION:CHECKIN: 01/02/2017\nCHECKOUT: 01/04/2017\nNIGHTS: 2\nPHONE:
+1 (222) 222-2222\nEMAIL: darren-3rntph9q5fqzbao2@test.bnb.com\nPROP
ERTY: Rittenhouse Square Cozy Studio Apartment\n
SUMMARY:Abe Lincoln (XXXXXX)
LOCATION:Rittenhouse Square Cozy Studio Apartment
END:VEVENT
END:VCALENDAR


Utilizing iCalendar Package here, I've tried the following:

from icalendar import Calendar

result = Calendar().from_ical(ical_content)


Result

VCALENDAR({u'CALSCALE': vText('GREGORIAN'), u'VERSION': vText('2.0'), u'PRODID': vText('-//Airbnb Inc//Hosting Calendar 0.8.8//EN')}, VEVENT({u'LOCATION': vText('Rittenhouse Square Cozy Studio Apartment'), u'DESCRIPTION': vText('CHECKIN: 11/10/2016\nCHECKOUT: 11/11/2016\nNIGHTS: 1\nPHONE: +1 (111) 111-1111\nEMAIL: gregory-66i5v1thmkdva1u4@test.bnb.com\nPROPERTY: Rittenhouse Square Cozy Studio Apartment\n'), u'UID': vText('-4sxkntmd7ehk-4ybn124l9iwr@bnbhome.com'), u'DTEND': <icalendar.prop.vDDDTypes object at 0x1041e5610>, u'DTSTART': <icalendar.prop.vDDDTypes object at 0x1041e58d0>, u'SUMMARY': vText('John Smith (XXXXXX)')}),VEVENT({u'LOCATION': vText('Rittenhouse Square Cozy Studio Apartment'), u'DESCRIPTION': vText('CHECKIN: 01/02/2017\nCHECKOUT: 01/04/2017\nNIGHTS: 2\nPHONE: +1 (222) 222-2222\nEMAIL: darren-3rntph9q5fqzbao2@test.bnb.com\nPROPERTY: Rittenhouse Square Cozy Studio Apartment\n'), u'UID': vText('-4sxkntmd7ehk-wps9gpnpzxkj@bnbhome.com'), u'DTEND': <icalendar.prop.vDDDTypes object at 0x10441c690>, u'DTSTART': <icalendar.prop.vDDDTypes object at 0x10441c6d0>, u'SUMMARY': vText('Abe Lincoln (XXXXXX)')}))


How can I transform this icalendar text into a json array?

Answer

Very nasty way, but do the job:

ical_content = """
BEGIN:VCALENDAR
PRODID;X-RICAL-TZSOURCE=TZINFO:-//Airbnb Inc//Hosting Calendar 0.8.8//EN
CALSCALE:GREGORIAN
VERSION:2.0
BEGIN:VEVENT
DTEND;VALUE=DATE:20161111
DTSTART;VALUE=DATE:20161110
UID:-4sxkntmd7ehk-4ybn124l9iwr@bnbhome.com
DESCRIPTION:CHECKIN: 11/10/2016\nCHECKOUT: 11/11/2016\nNIGHTS: 1\nPHONE:
 +1 (111) 111-1111\nEMAIL: gregory-66i5v1thmkdva1u4@test.bnb.com\nPRO
 PERTY: Rittenhouse Square Cozy Studio Apartment\n
SUMMARY:John Smith (XXXXXX)
LOCATION:Rittenhouse Square Cozy Studio Apartment
END:VEVENT
BEGIN:VEVENT
DTEND;VALUE=DATE:20170104
DTSTART;VALUE=DATE:20170102
UID:-4sxkntmd7ehk-wps9gpnpzxkj@bnbhome.com
DESCRIPTION:CHECKIN: 01/02/2017\nCHECKOUT: 01/04/2017\nNIGHTS: 2\nPHONE:
 +1 (222) 222-2222\nEMAIL: darren-3rntph9q5fqzbao2@test.bnb.com\nPROP
 ERTY: Rittenhouse Square Cozy Studio Apartment\n
SUMMARY:Abe Lincoln (XXXXXX)
LOCATION:Rittenhouse Square Cozy Studio Apartment
END:VEVENT
END:VCALENDAR
"""

import icalendar
from icalendar import Calendar

result = Calendar().from_ical(ical_content)

import json

r = []
for key, value in result.property_items():
    inner_d = {}
    if isinstance(value, icalendar.prop.vDDDTypes):
        inner_d[key] = value.dt.isoformat()
    else:
        inner_d[key] = value
    r.append(inner_d)

u = json.dumps(r, indent=4)
print(u)

And the output is:

[
    {
        "BEGIN": "VCALENDAR"
    }, 
    {
        "VERSION": "2.0"
    }, 
    {
        "PRODID": "-//Airbnb Inc//Hosting Calendar 0.8.8//EN"
    }, 
    {
        "CALSCALE": "GREGORIAN"
    }, 
    {
        "BEGIN": "VEVENT"
    }, 
    {
        "SUMMARY": "John Smith (XXXXXX)"
    }, 
    {
        "DTSTART": "2016-11-10"
    }, 
    {
        "DTEND": "2016-11-11"
    }, 
    {
        "UID": "-4sxkntmd7ehk-4ybn124l9iwr@bnbhome.com"
    }, 
    {
        "CHECKOUT": " 11/11/2016"
    }, 
    {
        "DESCRIPTION": "CHECKIN: 11/10/2016"
    }, 
    {
        "EMAIL": " gregory-66i5v1thmkdva1u4@test.bnb.com"
    }, 
    {
        "LOCATION": "Rittenhouse Square Cozy Studio Apartment"
    }, 
    {
        "NIGHTS": " 1"
    }, 
    {
        "PHONE": "+1 (111) 111-1111"
    }, 
    {
        "PROPERTY": " Rittenhouse Square Cozy Studio Apartment"
    }, 
    {
        "END": "VEVENT"
    }, 
    {
        "BEGIN": "VEVENT"
    }, 
    {
        "SUMMARY": "Abe Lincoln (XXXXXX)"
    }, 
    {
        "DTSTART": "2017-01-02"
    }, 
    {
        "DTEND": "2017-01-04"
    }, 
    {
        "UID": "-4sxkntmd7ehk-wps9gpnpzxkj@bnbhome.com"
    }, 
    {
        "CHECKOUT": " 01/04/2017"
    }, 
    {
        "DESCRIPTION": "CHECKIN: 01/02/2017"
    }, 
    {
        "EMAIL": " darren-3rntph9q5fqzbao2@test.bnb.com"
    }, 
    {
        "LOCATION": "Rittenhouse Square Cozy Studio Apartment"
    }, 
    {
        "NIGHTS": " 2"
    }, 
    {
        "PHONE": "+1 (222) 222-2222"
    }, 
    {
        "PROPERTY": " Rittenhouse Square Cozy Studio Apartment"
    }, 
    {
        "END": "VEVENT"
    }, 
    {
        "END": "VCALENDAR"
    }
]

Hope this help :)

Comments