0

I am building an API base on an Adyen Webhook and it seems like the JsonProperty is not being 'read'.

So I built a proof of concept by creating a model NotifyRequest.cs. For one of the properties I have defined [JsonProperty("NotificationItems")] but when posting JSON via Postman, the property is NULL. The "Live" property is being fed through.

NotifyRequest.cs:

using Newtonsoft.Json;
using System.Collections.Generic;
using web_external_payments.Models;

namespace web_external_payments.Models
{
    public class NotifyRequest
    {
    //    public NotifyRequest();

        public string Live { get; set; }
        [JsonProperty("NotificationItems")]
        public List<NotifyRequestItemContainer> NotifyItemContainers { get; set; }

        //public string ToJson();
        //public override string ToString();
    }
}

Controller:

[HttpPost]
public ActionResult Webhook(NotifyRequest notificationRequest)
{
    return View("Error");
}

JSON payload:

{
   "Live":"false",
   "NotificationItems":[
      {
         "NotificationItem":{
            "eventCode":"AUTHORISATION",
            "success":"true",
            "eventDate":"2019-06-28T18:03:50+01:00",
            "merchantAccountCode":"YOUR_MERCHANT_ACCOUNT",
            "pspReference": "7914073381342284",
            "merchantReference": "YOUR_REFERENCE"
         }
      }
   ]
}

However, if I instead post the JSON but use the name of the property instead (NotifyItemContainer), it does pick up the values in that JSON and is no longer NULL.

I am on .NET 4.7.2. Is there something with this particular .NET version where it's not using the JsonProperty or have I missed something completely?

enter image description here

  • 2
    Please do not post images of code or data, please paste them in as text. – Charlieface Jul 12 '22 at 11:33
  • 2
    Are you actually using Json.Net or are you using System.Text.Json? For the latter you need `JsonPropertyName`, see https://stackoverflow.com/questions/58271901/converting-newtonsoft-code-to-system-text-json-in-net-core-3-whats-equivalent – Charlieface Jul 12 '22 at 11:34
  • Added the code as text – Intermediary Developer Jul 12 '22 at 13:18
  • I am on .NET 4.7.2 and do not want to upgrade to .NET Core 3. So I'm using Newtonsoft.Json. – Intermediary Developer Jul 12 '22 at 13:22
  • I'm guessing you actually *aren't* using Json.Net even though you think you are. Try with `JsonPropertyName` instead and see what happens – Charlieface Jul 12 '22 at 13:59
  • I tried to add this, but it couldn't find this reference. `using System.Text.Json; // The type or namespace name 'Json' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)` I also set it to JsonPropertyName, but it couldn't find the reference either. `[JsonPropertyName("NotificationItems")] // The type or namespace name 'JsonPropertyName' could not be found (are you missing a using directive or an assembly reference?) ` – Intermediary Developer Jul 12 '22 at 14:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/246377/discussion-between-intermediary-developer-and-charlieface). – Intermediary Developer Jul 12 '22 at 14:36

1 Answers1

0

you will have to fix your NotifyRequest class according to json

public partial class NotifyRequest
{
    [JsonProperty("Live")]
    public bool Live { get; set; }

    [JsonProperty("NotificationItems")]
    public List<NotificationItemElement> NotificationItems { get; set; }
}

public partial class NotificationItemElement
{
    [JsonProperty("NotificationItem")]
    public NotificationItem NotificationItem { get; set; }
}

public partial class NotificationItem
{
    [JsonProperty("eventCode")]
    public string EventCode { get; set; }

    [JsonProperty("success")]
    public bool Success { get; set; }

    [JsonProperty("eventDate")]
    public DateTimeOffset EventDate { get; set; }

    [JsonProperty("merchantAccountCode")]
    public string MerchantAccountCode { get; set; }

    [JsonProperty("pspReference")]
    public string PspReference { get; set; }

    [JsonProperty("merchantReference")]
    public string MerchantReference { get; set; }
}
Serge
  • 40,935
  • 4
  • 18
  • 45