Problem when deserialize json with $

Asked

Viewed 56 times

2

Follows code json:

{
  "_id": {
    "$oid": "5b0f4a926724c71d945299f9"
  },
  "_class": "notificador.domain.entity.mongodb.WebhookNotification",
  "externalId": "EVE-XXXXXXXXXXXX",
  "resourceId": "TRA-XXXXXXXXXXXX",
  "accountId": "MPA-XXXXXXXXXXXX",
  "channelId": "APP-XXXXXXXXXXXX",
  "event": "TRANSFER.FAILED",
  "url": "{{url}}",
  "token": "{{token}}",
  "resource": {
    "createdAt": "2018-05-29T15:16:03.000-03",
    "amount": 0,
    "entries": [],
    "ownId": "00",
    "_links": {
      "self": {
        "href": "https://api.moip.com.br/v2/transfers/TRA-XXXXXXXXXXXX"
      }
    },
    "fee": 0,
    "cancellationDetails": {
      "cancelledBy": "BANK",
      "description": "CPF/CNPJ nao pertence ao titular da conta",
      "code": 5
    },
    "id": "TRA-XXXXXXXXXXXX",
    "transferInstrument": {
      "method": "BANK_ACCOUNT",
      "bankAccount": {
        "id": "BKA-XXXXXXXXXXXX",
        "agencyNumber": "0000",
        "holder": {
          "taxDocument": {
            "number": "{{NUMBER}}",
            "type": "CPF"
          },
          "fullname": "{{NAME}}"
        },
        "accountNumber": "000000",
        "accountCheckNumber": "0",
        "bankName": "XX",
        "type": "CHECKING",
        "bankNumber": "000"
      }
    },
    "events": [
      {
        "createdAt": "2018-05-29T15:16:03.000-03",
        "description": "Requested",
        "type": "TRANSFER.REQUESTED"
      },
      {
        "createdAt": "2018-05-30T00:00:00.000-03",
        "description": "Failed",
        "type": "TRANSFER.FAILED"
      }
    ],
    "updatedAt": "2018-05-30T00:00:00.000-03",
    "status": "FAILED"
  },
  "status": "SENT",
  "response": {
    "headers": {},
    "status": 200
  },
  "createdAt": {
    "$date": "2018-05-31T01:06:26.909Z"
  },
  "updatedAt": {
    "$date": "2018-05-31T01:06:27.231Z"
  },
  "webhookToFire": {
    "$ref": "webhook_to_fire",
    "$id": {
      "$oid": "5b0f4a926724c71d945299f8"
    }
  }
}

Follow code to deserialize:

return JsonConvert.DeserializeObject<Root>(json);

I get error:

Newtonsoft.Json.Jsonserializationexception: 'Additional content found in JSON Reference Object. A JSON Reference Object should only have a $ref Property. Path 'webhookToFire. $id', line 77, position 14.'

The problem is in the field $ref. How can I solve ?

Update

Follows Classes:

public partial class Root
{
    [JsonProperty("_id")]
    public Id Id { get; set; }

    [JsonProperty("_class")]
    public string Class { get; set; }

    [JsonProperty("externalId")]
    public string ExternalId { get; set; }

    [JsonProperty("resourceId")]
    public string ResourceId { get; set; }

    [JsonProperty("accountId")]
    public string AccountId { get; set; }

    [JsonProperty("channelId")]
    public string ChannelId { get; set; }

    [JsonProperty("event")]
    public string Event { get; set; }

    [JsonProperty("url")]
    public string Url { get; set; }

    [JsonProperty("token")]
    public string Token { get; set; }

    [JsonProperty("resource")]
    public Resource Resource { get; set; }

    [JsonProperty("status")]
    public string Status { get; set; }

    [JsonProperty("response")]
    public Response Response { get; set; }

    [JsonProperty("createdAt")]
    public AtedAt CreatedAt { get; set; }

    [JsonProperty("updatedAt")]
    public UpdatedAt UpdatedAt { get; set; }

    [JsonProperty("webhookToFire")]
    public WebhookToFire WebhookToFire { get; set; }
}

public partial class Createdat
{
    [JsonProperty("$date")]
    public DateTimeOffset Date { get; set; }
}

public partial class Id
{
    [JsonProperty("$oid")]
    public string Oid { get; set; }
}

public partial class Resource
{
    [JsonProperty("createdAt")]
    public string CreatedAt { get; set; }

    [JsonProperty("amount")]
    public long Amount { get; set; }

    [JsonProperty("entries")]
    public List<object> Entries { get; set; }

    [JsonProperty("ownId")]
    public string OwnId { get; set; }

    [JsonProperty("_links")]
    public Links Links { get; set; }

    [JsonProperty("fee")]
    public long Fee { get; set; }

    [JsonProperty("cancellationDetails")]
    public CancellationDetails CancellationDetails { get; set; }

    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("transferInstrument")]
    public TransferInstrument TransferInstrument { get; set; }

    [JsonProperty("events")]
    public List<Event> Events { get; set; }

    [JsonProperty("updatedAt")]
    public string UpdatedAt { get; set; }

    [JsonProperty("status")]
    public string Status { get; set; }
}

public partial class CancellationDetails
{
    [JsonProperty("cancelledBy")]
    public string CancelledBy { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("code")]
    public long Code { get; set; }
}

public partial class Event
{
    [JsonProperty("createdAt")]
    public string CreatedAt { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }
}

public partial class Links
{
    [JsonProperty("self")]
    public Self Self { get; set; }
}

public partial class Self
{
    [JsonProperty("href")]
    public Uri Href { get; set; }
}

public partial class TransferInstrument
{
    [JsonProperty("method")]
    public string Method { get; set; }

    [JsonProperty("bankAccount")]
    public BankAccount BankAccount { get; set; }
}

public partial class BankAccount
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("agencyNumber")]
    public string AgencyNumber { get; set; }

    [JsonProperty("holder")]
    public Holder Holder { get; set; }

    [JsonProperty("accountNumber")]
    public string AccountNumber { get; set; }

    [JsonProperty("accountCheckNumber")]
    [JsonConverter(typeof(ParseStringConverter))]
    public long AccountCheckNumber { get; set; }

    [JsonProperty("bankName")]
    public string BankName { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("bankNumber")]
    public string BankNumber { get; set; }
}

public partial class Holder
{
    [JsonProperty("taxDocument")]
    public TaxDocument TaxDocument { get; set; }

    [JsonProperty("fullname")]
    public string Fullname { get; set; }
}

public partial class TaxDocument
{
    [JsonProperty("number")]
    public string Number { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }
}

public partial class Response
{
    [JsonProperty("headers")]
    public Headers Headers { get; set; }

    [JsonProperty("status")]
    public long Status { get; set; }
}

public partial class Headers
{
}

public partial class WebhookToFire
{
    [JsonProperty("$ref")]
    public string Ref { get; set; }

    [JsonProperty("$id")]
    public Id Id { get; set; }
}
  • I don’t understand exactly what you want... You want to take the $ of the attributes of its JSON?

  • @Eduardoribeiro The way it is in the json code, it cannot deserialize. It seems to me that you have to remove $.

  • Post the class Root who created

  • @Guilhermenascimento ok, stand by.

  • Boy, I was trying to solve in JS, I even dreamt of this the night kkk But I see you already solved your problem, so it’s for the next kkk I guess I wouldn’t even solve your problem either.

1 answer

2


You need to configure in the classes in their properties JsonProperty (which can set for example the key name of the JSON) of as follows:

public class Rootobject
{
    [JsonProperty("_id")]
    public Id Id { get; set; }

    [JsonProperty("_class")]
    public string Class { get; set; }

    [JsonProperty("externalId")]
    public string ExternalId { get; set; }

    [JsonProperty("resourceId")]
    public string ResourceId { get; set; }

    [JsonProperty("accountId")]
    public string AccountId { get; set; }

    [JsonProperty("ChannelId")]
    public string channelId { get; set; }

    [JsonProperty("_event")]
    public string Event { get; set; }

    [JsonProperty("url")]
    public string Url { get; set; }

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

    [JsonProperty("Resource")]
    public Resource Resource { get; set; }

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

    [JsonProperty("response")]
    public Response Response { get; set; }

    [JsonProperty("createdAt")]
    public Createdat CreatedAt { get; set; }

    [JsonProperty("updatedAt")]
    public Updatedat UpdatedAt { get; set; }

    [JsonProperty("webhookToFire")]
    public Webhooktofire WebhookToFire { get; set; }
}

public class Id
{
    [JsonProperty("$oid")]
    public string Oid { get; set; }
}

public class Resource
{
    [JsonProperty("createdAt")]
    public DateTime CreatedAt { get; set; }

    [JsonProperty("amount")]
    public int Amount { get; set; }

    [JsonProperty("entries")]
    public object[] Entries { get; set; }

    [JsonProperty("ownId")]
    public string OwnId { get; set; }

    [JsonProperty("_links")]
    public Links Links { get; set; }

    [JsonProperty("fee")]
    public int Fee { get; set; }

    [JsonProperty("cancellationDetails")]
    public Cancellationdetails CancellationDetails { get; set; }

    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("transferInstrument")]
    public Transferinstrument TransferInstrument { get; set; }

    [JsonProperty("events")]
    public Event[] Events { get; set; }

    [JsonProperty("UpdatedAt")]
    public DateTime updatedAt { get; set; }

    [JsonProperty("status")]
    public string Status { get; set; }
}

public class Links
{
    [JsonProperty("self")]
    public Self Self { get; set; }
}

public class Self
{
    [JsonProperty("href")]
    public string Href { get; set; }
}

public class Cancellationdetails
{
    [JsonProperty("cancelledBy")]
    public string CancelledBy { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("code")]
    public int Code { get; set; }
}

public class Transferinstrument
{
    [JsonProperty("method")]
    public string Method { get; set; }

    [JsonProperty("bankAccount")]
    public Bankaccount BankAccount { get; set; }
}

public class Bankaccount
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("agencyNumber")]
    public string AgencyNumber { get; set; }

    [JsonProperty("holder")]
    public Holder Holder { get; set; }

    [JsonProperty("accountNumber")]
    public string AccountNumber { get; set; }

    [JsonProperty("accountCheckNumber")]
    public string AccountCheckNumber { get; set; }

    [JsonProperty("bankName")]
    public string BankName { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("bankNumber")]
    public string BankNumber { get; set; }
}

public class Holder
{
    [JsonProperty("taxDocument")]
    public Taxdocument TaxDocument { get; set; }

    [JsonProperty("fullname")]
    public string Fullname { get; set; }
}

public class Taxdocument
{
    [JsonProperty("number")]
    public string Number { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }
}

public class Event
{
    [JsonProperty("createdAt")]
    public DateTime CreatedAt { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }
}

public class Response
{
    [JsonProperty("headers")]
    public Headers Headers { get; set; }

    [JsonProperty("status")]
    public int Status { get; set; }
}

public class Headers
{
}

public class Createdat
{
    [JsonProperty("$date")]
    public DateTime Date { get; set; }
}

public class Updatedat
{
    [JsonProperty("$date")]
    public DateTime Date { get; set; }
}

public class Webhooktofire
{
    [JsonProperty("$ref")]
    public string Ref { get; set; }

    [JsonProperty("$id")]
    public Id Id { get; set; }
}   

After that also configure an option in settings(JsonSerializerSettings) to ignore the metadata JSON.NET makes in keys with the name of ref treat as normal property, example:

var settings = new JsonSerializerSettings();
settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;

var result = JsonConvert
      .DeserializeObject<Rootobject>(System.IO.File.ReadAllText("./json1.json"), settings);

Reference:

  • 1

    Man, you’re really good at this, solved my problem

  • 1

    I was trying to do it this way: PreserveReferencesHandling.None and it didn’t work.

  • 1

    You have to ignore ... take advantage because for version 3 of net core it will change ... @Matheusmiranda

  • 1

    You can configure direct by [JsonProperty()] ? I don’t think so ?

  • 1

    @Matheusmiranda does not have this configuration apparently, I at least did not find

  • 1

    Your answer will help a lot for those who will find this kind of problem $.

  • @Matheusmiranda if it’s the answer to your question don’t forget to vote

  • Yes I had forgotten

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.