0

I want to save my json code to mongoDB. But I dont save it to mongoDB. I'm getting an error.I searched a lot but couldn't figure it out.

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IList`1[KitapjetFormApp.Alias]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'User[1].Documents.Document.Alias.Name', line 43, position 19.'

Myjson Code is

{
  "User": [
    {
      "Identifier": "317933170",
      "Title": "--AT ŞA--N",
      "Type": "OZEL",
      "FirstCreationTime": "2021-03-11T19:14:53",
      "AccountType": "OZELENTEGRASYON",
      "Documents": {
        "Document": {
          "@type": "Invoice",
          "Alias": [
            {
              "Name": "urn:mail:gpk@g",
              "CreationTime": "2021-03-09T15:36:56"
            },
            {
              "Name": "urn:mail:hpk@h",
              "CreationTime": "2021-03-11T19:12:53"
            },
            {
              "Name": "urn:mail:rtypk@rty",
              "CreationTime": "2021-03-11T18:25:55"
            },
            {
              "Name": "urn:mail:qwspk@wqwq",
              "CreationTime": "2021-03-11T19:14:53"
            }
          ]
        }
      }
    },
    {
      "Identifier": "74102",
      "Title": "GID TAR ÜRÜN İNŞ YAPI MALZ HAY MADENCİLİK TAAH SAN VE TİC LTD ŞTİ",
      "Type": "OZEL",
      "FirstCreationTime": "2022-07-02T13:23:23",
      "AccountType": "GIBPORTAL",
      "Documents": {
        "Document": {
          "@type": "Invoice",
          "Alias": {
            "Name": "urn:mail:defaultpk@hotmail.com",
            "CreationTime": "2022-07-02T13:23:23"
          }
        }
      }
    },
    {
      "Identifier": "83303831",
      "Title": "HAFRİYAT İNŞAAT SANAYİ VE TİCARET LİMİTED ŞİRKETİ",
      "Type": "OZEL",
      "FirstCreationTime": "2022-07-01T12:27:14",
      "AccountType": "GIBPORTAL",
      "Documents": {
        "Document": {
          "@type": "Invoice",
          "Alias": {
            "Name": "urn:mail:defaultpk@hotmail.com",
            "CreationTime": "2022-07-01T12:27:14"
          }
        }
      }
    },
    {
      "Identifier": "0860986",
      "Title": "A YAPI MALZEMELER¦ VE ¦NÌAAT SANAY¦ T¦CARET L¦M¦TED ̦RKET¦",
      "Type": "OZEL",
      "FirstCreationTime": "2021-06-25T18:03:06",
      "AccountType": "GIBPORTAL",
      "Documents": {
        "Document": {
          "@type": "Invoice",
          "Alias": {
            "Name": "urn:mail:defaultpk@gmail.com",
            "CreationTime": "2021-06-25T18:03:06"
          }
        }
      }
    }
  ]
}

Myclass is

public class Alias
{
    public string Name { get; set; }
    public DateTime CreationTime { get; set; }

}
public class Document
{
    public string @type { get; set; }
    public IList<Alias> Alias { get; set; }

}
public class Documents
{
    public Document Document { get; set; }

}
public class User
{
    public string Identifier { get; set; }
    public string Title { get; set; }
    public string Type { get; set; }
    public DateTime FirstCreationTime { get; set; }
    public string AccountType { get; set; }
    public Documents Documents { get; set; }

}
public class MukellefRoot
{
    public IList<User> User { get; set; }

}

My code is here for mongodb insert

using (StreamReader file = File.OpenText(@"C:\mukellef\mukellef.json"))
{
        JsonSerializer serializer = new JsonSerializer();
        MukellefRoot mukellefler = (MukellefRoot)serializer.Deserialize(file, typeof(MukellefRoot));
        var mukellef = mukellefler.User.ToList();
        mukellefCollectionItems.InsertMany(mukellef);
}
Markus
  • 20,838
  • 4
  • 31
  • 55
matematikistan
  • 127
  • 1
  • 6
  • The problem happens before MongoDB comes into play. The JSON cannot be deserialized to your class structure. The problem is that `Alias` is an array in one case (this matches the definition as `IList` in `Document`) and for the next item it is a single object (this errors because Json.NET cannot match this with `IList`. This question might help: https://stackoverflow.com/q/27047875/642579 – Markus Aug 11 '22 at 05:40
  • Thanks. But I couldn't retrieve the data again. – matematikistan Aug 11 '22 at 16:08
  • I found the solution using Newtonsoft.Json; dynamic results = JsonConvert.DeserializeObject(YOUR_JSON); – matematikistan Aug 12 '22 at 05:46

1 Answers1

0

I found the solution

using Newtonsoft.Json;

dynamic results = JsonConvert.DeserializeObject<dynamic>(YOUR_JSON);
matematikistan
  • 127
  • 1
  • 6