C
Join ServerC#
help
Making an azure Function to merge two JSON objects
Mmalkav9/23/2022
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:
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
Does this mean I should be using
currently I'm trying this:
I would love some assistance 😅 I'm stuck at this for 2 days now
{ "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;}
}
I would love some assistance 😅 I'm stuck at this for 2 days now
Mmalkav9/23/2022
Currently it's breaking on the initial
JsonConvert.DeserializeObject<Inputs>(requestBody)
TTvde19/23/2022
what's the input you're sending?
Mmalkav9/23/2022
well I have this atm:
and what I get on the Azure Function portal when I try to test with the following bit of Json
I get this error shown in the logs:
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"],
}
I get this error shown in the logs:
Could not cast or convert from System.String to MergeObjects.Inputs.
Mmalkav9/23/2022
(Edited the JSON input I use)
TTvde19/23/2022
that's a weird error message
Mmalkav9/23/2022
yea, kind of is innit 😅 hence my question
TTvde19/23/2022
what library is JsonConvert?
Mmalkav9/23/2022
Newtonsoft.Json
TTvde19/23/2022
you might want to try it with
System.Text.Json
TTvde19/23/2022
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>();
}
Mmalkav9/23/2022
I've got it solved!
I had an additional
I had an additional
,
at the end of my JSON that was the entire issue... 