Skip to content

Commit 47a17d0

Browse files
committed
Add GetStructuredData method to JSON handlers
Introduce a new method `GetStructuredData` in the `ExtractOperationHandler`, `MapOperationHandler`, and `TransformOperationHandler` classes. This method converts JSON tokens into a structured format as a list of dictionaries, enhancing the `PluginContext` with a new `StructuredData` property for better data representation and processing. #7
1 parent 3a7fe83 commit 47a17d0

3 files changed

Lines changed: 68 additions & 6 deletions

File tree

src/Services/ExtractOperationHandler.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,50 @@ public object Handle(JToken json, InputParameter inputParameter)
2323
var tokens = json.SelectTokens(path).ToList();
2424
string filename = $"{_guidProvider.NewGuid()}.json";
2525

26+
List<Dictionary<string, object>>? structuredData = null;
2627
if (tokens.Count == 0)
2728
{
2829
return new PluginContext(filename, "Data")
2930
{
3031
Format = "Json",
31-
Content = "null"
32+
Content = "null",
33+
StructuredData = null
3234
};
3335
}
3436

3537
JToken result = tokens.Count == 1
3638
? tokens[0]
3739
: new JArray(tokens);
3840

41+
structuredData = GetStructuredData(result);
42+
3943
return new PluginContext(filename, "Data")
4044
{
4145
Format = "Json",
42-
Content = result.ToString(inputParameter.Indented ? Formatting.Indented : Formatting.None)
46+
Content = result.ToString(inputParameter.Indented ? Formatting.Indented : Formatting.None),
47+
StructuredData = structuredData
4348
};
4449
}
50+
51+
private List<Dictionary<string, object>>? GetStructuredData(JToken token)
52+
{
53+
if (token is JArray arr && arr.Count > 0)
54+
{
55+
var list = new List<Dictionary<string, object>>();
56+
foreach (var item in arr)
57+
{
58+
if (item is JObject obj)
59+
list.Add(obj.Properties().ToDictionary(p => p.Name, p => (object)p.Value.Type.ToString()));
60+
}
61+
return list.Count > 0 ? list : null;
62+
}
63+
if (token is JObject obj2)
64+
{
65+
return new List<Dictionary<string, object>>
66+
{
67+
obj2.Properties().ToDictionary(p => p.Name, p => (object)p.Value.Type.ToString())
68+
};
69+
}
70+
return null;
71+
}
4572
}

src/Services/MapOperationHandler.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,27 @@ public object Handle(JToken json, InputParameter inputParameter)
2020
throw new InvalidOperationException("Mappings not defined in specifications.");
2121

2222
object mappedResult;
23+
List<Dictionary<string, object>>? structuredData = null;
2324

2425
if (json is JArray array)
2526
{
26-
mappedResult = array
27+
var mappedList = array
2728
.OfType<JObject>()
2829
.Select(obj => MapSingleObject(obj, inputParameter.Mappings))
2930
.ToList();
31+
mappedResult = mappedList;
32+
structuredData = mappedList
33+
.Select(obj => obj.ToDictionary(kvp => kvp.Key, kvp => (object)(kvp.Value?.GetType()?.Name ?? "null")))
34+
.ToList();
3035
}
3136
else if (json is JObject obj)
3237
{
33-
mappedResult = MapSingleObject(obj, inputParameter.Mappings);
38+
var mappedObj = MapSingleObject(obj, inputParameter.Mappings);
39+
mappedResult = mappedObj;
40+
structuredData = new List<Dictionary<string, object>>
41+
{
42+
mappedObj.ToDictionary(kvp => kvp.Key, kvp => (object)(kvp.Value?.GetType()?.Name ?? "null"))
43+
};
3444
}
3545
else
3646
{
@@ -42,7 +52,8 @@ public object Handle(JToken json, InputParameter inputParameter)
4252
return new PluginContext(filename, "Data")
4353
{
4454
Format = "Json",
45-
Content = convertedJson
55+
Content = convertedJson,
56+
StructuredData = structuredData
4657
};
4758
}
4859

src/Services/TransformOperationHandler.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,37 @@ public object Handle(JToken json, InputParameter inputParameter)
3232

3333
var transformedResult = result.ToString(inputParameter.Indented ? Formatting.Indented : Formatting.None);
3434
string filename = $"{_guidProvider.NewGuid()}.json";
35+
var structuredData = GetStructuredData(result);
3536
return new PluginContext(filename, "Data")
3637
{
3738
Format = "Json",
38-
Content = transformedResult
39+
Content = transformedResult,
40+
StructuredData = structuredData
3941
};
4042
}
4143

44+
private List<Dictionary<string, object>>? GetStructuredData(JToken token)
45+
{
46+
if (token is JArray arr && arr.Count > 0)
47+
{
48+
var list = new List<Dictionary<string, object>>();
49+
foreach (var item in arr)
50+
{
51+
if (item is JObject obj)
52+
list.Add(obj.Properties().ToDictionary(p => p.Name, p => (object)p.Value.Type.ToString()));
53+
}
54+
return list.Count > 0 ? list : null;
55+
}
56+
if (token is JObject obj2)
57+
{
58+
return new List<Dictionary<string, object>>
59+
{
60+
obj2.Properties().ToDictionary(p => p.Name, p => (object)p.Value.Type.ToString())
61+
};
62+
}
63+
return null;
64+
}
65+
4266
private JObject FlattenJson(JObject input)
4367
{
4468
var result = new JObject();

0 commit comments

Comments
 (0)