0

New to C# and JSON but basically I have to use a C# script component in SSIS to extract data from a web service. I was able to do it initially with a different JSON format but this one includes an array. I get the following error:

"Type 'Rootobject' is not supported for deserialization of an array."

Below is how I call the Deserializer:

//Deserialize our JSON
 JavaScriptSerializer sr = new 
 jsonResponse = sr.Deserialize<Rootobject>(responseFromServer);

Here is my JSON structure (not quite all since fairly large):

[  
   {  
      "status":"SUCCESS",
      "object":{  
         "responseStatusCode":0,
         "productId":"35100003",
         "cansimId":"251-0008",
         "cubeTitleEn":"Average counts of young persons in provincial and territorial correctional services",
         "cubeTitleFr":"Comptes moyens des adolescents dans les services correctionnels provinciaux et territoriaux",
         "cubeStartDate":"1997-01-01",
         "cubeEndDate":"2017-01-01",
         "frequencyCode":12,
         "nbSeriesCube":174,
         "nbDatapointsCube":3468,
         "releaseTime":"2019-05-09T08:30",
         "archiveStatusCode":"2",
         "archiveStatusEn":"CURRENT - a cube available to the public and that is current",
         "archiveStatusFr":"ACTIF - un cube qui est disponible au public et qui est toujours mise a jour",
         "subjectCode":[  
            "350102",
            "4211"
         ],
         "surveyCode":[  
            "3313"
         ],
         "dimension":[  ],
         "footnote":[  ],
         "correction":[  

         ]
      }
   }
]

Finally here is my Class structure obtained through Paste Special in Visual Studio:

public class Rootobject
{

    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public string status { get; set; }
    public Object _object { get; set; }
}

public class Object
{
    public int responseStatusCode { get; set; }
    public string productId { get; set; }
    public string cansimId { get; set; }
    public string cubeTitleEn { get; set; }
    public string cubeTitleFr { get; set; }
    public string cubeStartDate { get; set; }
    public string cubeEndDate { get; set; }
    public int frequencyCode { get; set; }
    public int nbSeriesCube { get; set; }
    public int nbDatapointsCube { get; set; }
    public string releaseTime { get; set; }
    public string archiveStatusCode { get; set; }
    public string archiveStatusEn { get; set; }
    public string archiveStatusFr { get; set; }
    public string[] subjectCode { get; set; }
    public string[] surveyCode { get; set; }
    public Dimension[] dimension { get; set; }
    public Footnote[] footnote { get; set; }
    public object[] correction { get; set; }
}

public class Dimension
{
    public int dimensionPositionId { get; set; }
    public string dimensionNameEn { get; set; }
    public string dimensionNameFr { get; set; }
    public bool hasUom { get; set; }
    public Member[] member { get; set; }
}

public class Member
{
    public int memberId { get; set; }
    public int? parentMemberId { get; set; }
    public string memberNameEn { get; set; }
    public string memberNameFr { get; set; }
    public string classificationCode { get; set; }
    public string classificationTypeCode { get; set; }
    public int? geoLevel { get; set; }
    public int? vintage { get; set; }
    public int terminated { get; set; }
    public int? memberUomCode { get; set; }
}

public class Footnote
{
    public int footnoteId { get; set; }
    public string footnotesEn { get; set; }
    public string footnotesFr { get; set; }
    public Link link { get; set; }
}

public class Link
{
    public int footnoteId { get; set; }
    public int dimensionPositionId { get; set; }
    public int memberId { get; set; }
}

I know the problem lies within how I call the Deserialize<Rootobject> and different data types but I wasn't able to find the solution. Any suggestions are appreciated.

AV

1
  • You don't have a Rootobject you have an array of Class1. This jsonResponse = sr.Deserialize<Rootobject>(responseFromServer); should be jsonResponse = sr.Deserialize<Class1[]>(responseFromServer); Commented Jul 30, 2019 at 18:38

1 Answer 1

0

Try deserializing directly to a list of Class1.

jsonResponse = sr.Deserialize<List<Class1>>(responseFromServer);

Also, don't use Object as your class name. That's a really bad practice.

You can control how JSON.NET serializes/deserializes a property as shown here: How can I change property names when serializing with Json.net?

e.g.

[JsonProperty(PropertyName = "object")]
public class MyObject
{
}
Sign up to request clarification or add additional context in comments.

4 Comments

joelc - thank you for your answer. Unfortunately, I cannot use Newton JSON dll with SSIS in our current setup. What eventually work was to add <List> in front of <RootObject> everywhere. Can I rename the classes w/o breaking the structure using Microsoft build in references?
Hi @adrianvas12 I wouldn't be able to answer that, as I only use JSON.NET for serialization/deserialization, and haven't worked extensively enough with System.Web.Script.Serialization. It appears it does not have a way to decorate a class/member with a specific property name. You may want to check this solution for more information, seems relevant to you: stackoverflow.com/questions/32487483/…
In general I'd really recommend looking into ways to move over to JSON.NET. It's a pretty incredible library. If there's a technical limitation that prevents you from using it (you mention SSIS), you may want to ask another question and see if there's a solution for that. JSON.NET is literally that much better than the built-in serializer. Cheers
Thank you! I will see if adding JSON.NET to SSIS is an option. I know that we need to install and register a custom DLL and that may not be an on our production machine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.