C
C#•3y ago
malkav

Making an azure Function to merge two JSON objects

I'm trying to make an Azure Function to merge two JSON objects, but apparently azure Functions don't like the idea of adding a JSON like this:
{ "main_object": "{ \"name\": \"some name\", \"more_properties\": \"values\" }" }
{ "main_object": "{ \"name\": \"some name\", \"more_properties\": \"values\" }" }
so now I'm trying to figure out how to make sure I can pass two json objects of which I not know their data into the request body. My parameters on the request body should be (JSON)main_object, (JSON)secondary_object, (anything, so JToken??)matching_property, (string[])merge_properties Does this mean I should be using JToken? currently I'm trying this:
[FunctionName("MergeSingleAsync")]
public static async Task<IActionResult> RunAsync(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
HttpRequest req, ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
Inputs data = JsonConvert.DeserializeObject<Inputs>(requestBody);
// ...
}

class Inputs
{
[JsonProperty("main_object")] public string MainObject {get;set;} // Should be JToken??
[JsonProperty("secondary_object")] public string SecondaryObject {get;set;} // Should be JToken??
[JsonProperty("matching_property")] public JToken MatchingProperty {get;set;}
[JsonProperty("merge_properties")] public string[] MergeProperties {get;set;}
}
[FunctionName("MergeSingleAsync")]
public static async Task<IActionResult> RunAsync(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
HttpRequest req, ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
Inputs data = JsonConvert.DeserializeObject<Inputs>(requestBody);
// ...
}

class Inputs
{
[JsonProperty("main_object")] public string MainObject {get;set;} // Should be JToken??
[JsonProperty("secondary_object")] public string SecondaryObject {get;set;} // Should be JToken??
[JsonProperty("matching_property")] public JToken MatchingProperty {get;set;}
[JsonProperty("merge_properties")] public string[] MergeProperties {get;set;}
}
I would love some assistance 😅 I'm stuck at this for 2 days now
9 Replies
malkav
malkavOP•3y ago
Currently it's breaking on the initial JsonConvert.DeserializeObject<Inputs>(requestBody)
Tvde1
Tvde1•3y ago
what's the input you're sending?
malkav
malkavOP•3y ago
well I have this atm:
public class Inputs
{
[JsonProperty("main_object")] public string MainObject { get; set; }
[JsonProperty("secondary_object")] public string SecondaryObject { get; set; }
[JsonProperty("matching_property")] public JValue MatchingProperty { get; set; }
[JsonProperty("merge_properties")] public string[] MergeProperties { get; set; } = Array.Empty<string>();
}

[FunctionName("MergeSingleAsync")]
public static async Task<IActionResult> RunAsync(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
HttpRequest req, ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
Inputs data = JsonConvert.DeserializeObject<Inputs>(requestBody);
log.LogDebug(data.MainObject);
// ... more code (irrelevant because it breaks before I can even touch it)
return new OkObjectResult(JsonConvert.SerializeObject(newObject));
}
public class Inputs
{
[JsonProperty("main_object")] public string MainObject { get; set; }
[JsonProperty("secondary_object")] public string SecondaryObject { get; set; }
[JsonProperty("matching_property")] public JValue MatchingProperty { get; set; }
[JsonProperty("merge_properties")] public string[] MergeProperties { get; set; } = Array.Empty<string>();
}

[FunctionName("MergeSingleAsync")]
public static async Task<IActionResult> RunAsync(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
HttpRequest req, ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
Inputs data = JsonConvert.DeserializeObject<Inputs>(requestBody);
log.LogDebug(data.MainObject);
// ... more code (irrelevant because it breaks before I can even touch it)
return new OkObjectResult(JsonConvert.SerializeObject(newObject));
}
and what I get on the Azure Function portal when I try to test with the following bit of Json
{
"main_object": "{ \"name\": \"some name\", \"id\": \"123\" }",
"secondary_object": "{ \"phone\": \"1234567\", \"id\": \"123\" }",
"matching_property": "id",
"merge_properties": ["phone"],
}
{
"main_object": "{ \"name\": \"some name\", \"id\": \"123\" }",
"secondary_object": "{ \"phone\": \"1234567\", \"id\": \"123\" }",
"matching_property": "id",
"merge_properties": ["phone"],
}
I get this error shown in the logs:
Could not cast or convert from System.String to MergeObjects.Inputs.
Could not cast or convert from System.String to MergeObjects.Inputs.
(Edited the JSON input I use)
Tvde1
Tvde1•3y ago
that's a weird error message
malkav
malkavOP•3y ago
yea, kind of is innit 😅 hence my question
Tvde1
Tvde1•3y ago
what library is JsonConvert?
Tvde1
Tvde1•3y ago
you might want to try it with System.Text.Json
using System.Text.Json;
using System.Text.Json.Serialization;

var requestBody = @"
{
""main_object"": ""{ \""name\"": \""some name\"", \""id\"": \""123\"" }"",
""secondary_object"": ""{ \""phone\"": \""1234567\"", \""id\"": \""123\"" }"",
""matching_property"": ""id"",
""merge_properties"": [""phone""]
}";

Inputs data = JsonSerializer.Deserialize<Inputs>(requestBody);

data.Dump();

public class Inputs
{
[JsonPropertyName("main_object")] public string MainObject { get; set; }
[JsonPropertyName("secondary_object")] public string SecondaryObject { get; set; }
[JsonPropertyName("matching_property")] public string MatchingProperty { get; set; }
[JsonPropertyName("merge_properties")] public string[] MergeProperties { get; set; } = Array.Empty<string>();
}
using System.Text.Json;
using System.Text.Json.Serialization;

var requestBody = @"
{
""main_object"": ""{ \""name\"": \""some name\"", \""id\"": \""123\"" }"",
""secondary_object"": ""{ \""phone\"": \""1234567\"", \""id\"": \""123\"" }"",
""matching_property"": ""id"",
""merge_properties"": [""phone""]
}";

Inputs data = JsonSerializer.Deserialize<Inputs>(requestBody);

data.Dump();

public class Inputs
{
[JsonPropertyName("main_object")] public string MainObject { get; set; }
[JsonPropertyName("secondary_object")] public string SecondaryObject { get; set; }
[JsonPropertyName("matching_property")] public string MatchingProperty { get; set; }
[JsonPropertyName("merge_properties")] public string[] MergeProperties { get; set; } = Array.Empty<string>();
}
malkav
malkavOP•3y ago
I've got it solved! I had an additional , at the end of my JSON that was the entire issue... <:picard_facepalm:616692703685509130>

Did you find this page helpful?