Преобразование Microsoft Graph JSON в CSV с помощью Cinchoo ETL
Я пытаюсь преобразовать файл JSON (Microsoft.Graph.Event) в файл CSV. Для этого я использую Cinchoo ETL. Вот URL, о котором я говорю:
https://www.codeproject.com/Articles/1193650/Cinchoo-ETL-Quick-Start-Converting-JSON-to-CSV-Fil
Вот мой код:
using (var csv = new ChoCSVWriter(path + calendarId + ".csv").WithFirstLineHeader())
{
using (var json = new ChoJSONReader(path + calendarId + ".json")
.WithField("id")
.WithField("iCalUId")
.WithField("isAllDay")
.WithField("isCancelled")
.WithField("isOrganizer")
.WithField("isOnlineMeeting")
.WithField("onlineMeetingProvider")
.WithField("type")
.WithField("startTime", jsonPath: "$.start.dateTime")
.WithField("endTime", jsonPath: "$.end.dateTime")
.WithField("location", jsonPath: "$.location.displayname")
.WithField("locationType", jsonPath: "$.location.locationType")
.WithField("organizer", jsonPath: "$.organizer.emailAddress.name")
.WithField("recurrence", jsonPath: "$.recurrence.pattern.type")
)
{
csv.Write(json);
}
}
Хотя у меня есть CSV, и большинство заголовков и значений верны, некоторые из них странные. Некоторые заголовки имеют на обороте "_0", а некоторые значения представляют собой просто повторение предыдущего столбца, а не то, что должно быть. снимок файла csv
Я проверил файл JSON, который написал заранее, но они были в порядке. Я использую.Net Core 3.1.
Я только новичок, и я буду благодарен за любую помощь или совет. Спасибо.
ИЗМЕНИТЬ, добавив снимок CSV, в котором некоторые значения являются просто повторением предыдущего столбца startTime вместо того, что должно быть.
Часть файла JSON
"start": {
"dateTime": "2020-05-17T00:00:00.0000000",
"timeZone": "UTC",
"@odata.type": "microsoft.graph.dateTimeTimeZone"
},
"end": {
"dateTime": "2020-05-18T00:00:00.0000000",
"timeZone": "UTC",
"@odata.type": "microsoft.graph.dateTimeTimeZone"
},
"location": {
"displayName": "asdfads",
"locationType": "default",
"uniqueId": "b0fd5377-937d-4fb2-b70a-0a696972b46c",
"uniqueIdType": "locationStore",
"@odata.type": "microsoft.graph.location"
},
1 ответ
Вот и все, я взял образец JSON из
https://docs.microsoft.com/en-us/graph/api/calendar-post-events?view=graph-rest-1.0&tabs=http
использовался для тестирования.
Вот код с использованием ChoETL v1.2.0.2 (последняя версия):
StringBuilder csv = new StringBuilder();
using (var w = new ChoCSVWriter(csv)
.WithFirstLineHeader()
)
{
using (var r = new ChoJSONReader(@"C:\Projects\GitHub\ChoETL\src\Test\ChoJSONReaderTest\sample41.json")
.WithField("id")
.WithField("iCalUId")
.WithField("isAllDay")
.WithField("isCancelled")
.WithField("isOrganizer")
.WithField("isOnlineMeeting")
.WithField("onlineMeetingProvider")
.WithField("type")
.WithField("startTime", jsonPath: "$.start.dateTime", isArray: false)
.WithField("endTime", jsonPath: "$.end.dateTime", isArray: false)
.WithField("location", jsonPath: "$.location.displayname")
.WithField("locationType", jsonPath: "$.location.locationType", isArray: false)
.WithField("organizer", jsonPath: "$.organizer.emailAddress.name", isArray: false)
.WithField("recurrence", jsonPath: "$.recurrence.pattern.type")
)
{
w.Write(r);
}
}
Console.WriteLine(csv.ToString());
Выход:
id,iCalUId,isAllDay,isCancelled,isOrganizer,isOnlineMeeting,onlineMeetingProvider,type,startTime,endTime,location,locationType,organizer,recurrence
AAMkAGViNDU7zAAAAA7zAAAZb2ckAAA=,040000008200E641B4C,False,False,True,False,unknown,singleInstance,3/15/2019 12:00:00 PM,3/15/2019 2:00:00 PM,,default,Megan Bowen,
ОБНОВЛЕНИЕ: вот обновленный код для изменения порядка полей и подсчета посетителей.
StringBuilder csv = new StringBuilder();
using (var w = new ChoCSVWriter(csv)
.WithFirstLineHeader()
)
{
using (var r = new ChoJSONReader(@"C:\Projects\GitHub\ChoETL\src\Test\ChoJSONReaderTest\sample41.json")
.WithField("startTime", jsonPath: "$.start.dateTime", isArray: false)
.WithField("endTime", jsonPath: "$.end.dateTime", isArray: false)
.WithField("id")
.WithField("iCalUId")
.WithField("isAllDay")
.WithField("isCancelled")
.WithField("isOrganizer")
.WithField("isOnlineMeeting")
.WithField("onlineMeetingProvider")
.WithField("type")
.WithField("location", jsonPath: "$.location.displayname")
.WithField("locationType", jsonPath: "$.location.locationType", isArray: false)
.WithField("organizer", jsonPath: "$.organizer.emailAddress.name", isArray: false)
.WithField("recurrence", jsonPath: "$.recurrence.pattern.type")
.WithField("attendees", jsonPath: "$.attendees[*]", valueConverter: o => ((IList)o).Count)
)
{
w.Write(r);
}
}
Console.WriteLine(csv.ToString());