-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCosmosDBCsvStringToJsonConverter.cs
74 lines (67 loc) · 3.07 KB
/
CosmosDBCsvStringToJsonConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.Azure.Documents.Tools.POC.BlobToCosmosDB
{
internal sealed class CosmosDBCsvStringToJsonConverter
{
public static string JsonTextFromCsv(string Csv, string[] fieldNames, string[] commaSeparatedFieldValues, Dictionary<string, string> stringFields, List<string> pKFields)
{
// Extract the name of the partition key field
string PKFieldName = ConfigurationManager.AppSettings["PartitionKeyFieldName"];
// Maintain the values of the fields that need to be used to create a composite key
Dictionary<string, string> PKFieldMapping = new Dictionary<string, string>();
PKFieldMapping.Add(PKFieldName, "");
// Maintain a map of field, value pairs for each line in the CSV file to ingest into Cosmos DB
Dictionary<string, object> JObjectMapping = new Dictionary<string, object>();
for (int EachFieldIndex = 0; EachFieldIndex < fieldNames.Length; EachFieldIndex++)
{
if (pKFields.Contains(fieldNames[EachFieldIndex]))
{
if (!string.IsNullOrEmpty(commaSeparatedFieldValues[EachFieldIndex]) && !commaSeparatedFieldValues[EachFieldIndex].Equals("\"\""))
{
PKFieldMapping[PKFieldName] = string.Concat(PKFieldMapping[PKFieldName], "_", commaSeparatedFieldValues[EachFieldIndex]);
}
}
else
{
// Treat string fields
if (stringFields.ContainsKey(fieldNames[EachFieldIndex]))
{
if (commaSeparatedFieldValues[EachFieldIndex].Equals("\"\""))
{
JObjectMapping.Add(fieldNames[EachFieldIndex], "\"\"");
}
else
{
JObjectMapping.Add(fieldNames[EachFieldIndex], commaSeparatedFieldValues[EachFieldIndex]);
}
}
else
{
// Treat numeric fields
if (commaSeparatedFieldValues[EachFieldIndex].Length == 0)
{
JObjectMapping.Add(fieldNames[EachFieldIndex], 0.0);
}
else
{
double valueToWrite = int.Parse(commaSeparatedFieldValues[EachFieldIndex]);
JObjectMapping.Add(fieldNames[EachFieldIndex], valueToWrite);
}
}
}
}
if (pKFields.Count > 0)
{
JObjectMapping.Add(PKFieldName, PKFieldMapping[PKFieldName]);
}
return JsonConvert.SerializeObject(JObjectMapping);
}
}
}