web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

JSON Deserialization returning null Contract class

(0) ShareShare
ReportReport
Posted on by 20

Hello community, I am trying to deserialize a JSON but when doing so, the class object remains null. I would like to know what I am doing wrong.

My Json object class:

[DataContract]
class JSONContractEnvFac
{
    boolean success;
    String50 globalDocumentId;
    String50 countryDocumentId;
    JSONContractEnvFac_OD otherData;
    List messages;
    String50 responseValue;
    int code;
    String50 description;
    String50 errorException;
    int folio;


    [DataMember('Success')]
    public boolean parmSuccess(boolean _success = success)
    {
        success = _success;
        return success;
    }

    [DataMember('GlobalDocumentId')]
    public String50 parmGlobalDocumentId(String50 _globalDocumentId = globalDocumentId)
    {
        globalDocumentId = _globalDocumentId;
        return globalDocumentId;
    }

    [DataMember('CountryDocumentId')]
    public String50 parmCountryDocumentId(String50 _countryDocumentId = countryDocumentId)
    {
        countryDocumentId = _countryDocumentId;
        return countryDocumentId;
    }

    [DataMember('OtherData')]
    public JSONContractEnvFac_OD parmOtherData(JSONContractEnvFac_OD _otherData = otherData)
    {
        otherData = _otherData;
        return otherData;
    }

    [DataMember('Messages'),
        DataCollection(Types::String)]
    public List parmMessages(List _messages = messages)
    {
        messages = _messages;
        return messages;
    }

    [DataMember('ResponseValue')]
    public String50 parmResponseValue(String50 _responseValue = responseValue)
    {
        responseValue = _responseValue;
        return responseValue;
    }

    [DataMember('Code')]
    public int parmCode(int _code = code)
    {
        code = _code;
        return code;
    }

    [DataMember('Description')]
    public String50 parmDescription(String50 _description = description)
    {
        description = _description;
        return description;
    }

    [DataMember('ErrorException')]
    public String50 parmErrorException(String50 _errorException = errorException)
    {
        errorException = _errorException;
        return errorException;
    }

    [DataMember('Folio')]
    public int parmFolio(int _folio = folio)
    {
        folio = _folio;
        return folio;
    }

}

[DataContract]
class JSONContractEnvFac_OD
{
    String50 country;
    String50 certifier;
    String50 authorityTimeStamp;
    String50 barCodeText;


    [DataMember('Country')]
    public String50 parmCountry(String50 _country = country)
    {
        country = _country;
        return country;
    }

    [DataMember('Certifier')]
    public String50 parmCertifier(String50 _certifier = certifier)
    {
        certifier = _certifier;
        return certifier;
    }

    [DataMember('AuthorityTimeStamp')]
    public String50 parmAuthorityTimeStamp(String50 _authorityTimeStamp = authorityTimeStamp)
    {
        authorityTimeStamp = _authorityTimeStamp;
        return authorityTimeStamp;
    }

    [DataMember('BarCodeText')]
    public String50 parmBarCodeText(String50 _barCodeText = barCodeText)
    {
        barCodeText = _barCodeText;
        return barCodeText;
    }

}

and here I try to deserialize:

            httpHeader = new System.Net.WebHeaderCollection();
            new InteropPermission(InteropKind::ClrInterop).assert();
            clrObj = System.Net.WebRequest::Create(ex_FacElectronica_Config.UrlBase ex_FacElectronica_Config.UrlEnviarFactura);
            request = clrObj;
            // adding headers
            httpHeader.Add("Authorization", code64);
            request.set_Headers(httpHeader);
            request.Method = "POST";
            request.ContentType = "application/json";
            requestStream = request.GetRequestStream();
            streamWriter = new System.IO.StreamWriter(request.GetRequestStream());

            var data = '{' 
                        '"mapping": "' ex_FacElectronica_Config.Mapping '",' 
                        '"sign": "true",' 
                        '"defaultCertificate": "true",' 
                        '"async": "false",' 
                        '"fileContent": "' factXML '"' 
                        '}' 
                        '';   

            streamWriter.Write(data); // writing JSON
            streamWriter.Flush();
            streamWriter.Close();
            response = request.GetResponse();
            System.IO.StreamReader streamRead = new System.IO.StreamReader(response.GetResponseStream());
            json = streamRead.ReadToEnd();
			
            JSONContractEnvFac jSONContractEnvFac = new JSONContractEnvFac();
            jSONContractEnvFac = FormJsonSerializer::deserializeObject(classnum(JSONContractEnvFac),json);

What it returns and saves in the JSON str is:

1663.capture.png

"\"{\\\"Success\\\":false,\\\"GlobalDocumentId\\\":\\\"00000000-0000-0000-0000-000000000000\\\",\\\"CountryDocumentId\\\":\\\"01800020146001001000001422021083019605712885\\\",\\\"OtherData\\\":null,\\\"Messages\\\":null,\\\"ResponseValue\\\":\\\"\\\",\\\"Code\\\":99,\\\"Description\\\":\\\"Error de validación.\\\",\\\"ErrorException\\\":\\\"The element 'gEmis' in namespace 'http://ekuatia.set.gov.py/sifen/xsd' has incomplete content. List of possible elements expected: 'gActEco' in namespace 'http://ekuatia.set.gov.py/sifen/xsd'.\\\",\\\"Folio\\\":0}\""

Can you help me please

I have the same question (0)
  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    Please give us your JSON in a more readable way.

    I think you should expand the options (between the magnifier icon and your selection in the screenshot) and choose the text visualizer to copy the text.

  • Osval25 Profile Picture
    20 on at

    Hi Martin, thank you very much for the quick response. Here is what he asked me.

    as a text viewer:

    8623.capture.png

  • Verified answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    Now please format it and paste through Insert > Code (with Language = JSON) to the forum.

    Having your JSON code as a picture doesn't allow us to copy it and try it.

  • Osval25 Profile Picture
    20 on at

    "{\"Success\":false,\"GlobalDocumentId\":\"00000000-0000-0000-0000-000000000000\",\"CountryDocumentId\":\"01800020146001001000001422021083019605712885\",\"OtherData\":null,\"Messages\":null,\"ResponseValue\":\"\",\"Code\":99,\"Description\":\"Error de validación.\",\"ErrorException\":\"The element 'gEmis' in namespace 'http://ekuatia.set.gov.py/sifen/xsd' has incomplete content. List of possible elements expected: 'gActEco' in namespace 'http://ekuatia.set.gov.py/sifen/xsd'.\",\"Folio\":0}"

    with the visual studio JSON viewer I get this: (I also paste it as JSON code from the forum)

    "{"Success":false,"GlobalDocumentId":"00000000-0000-0000-0000-000000000000","CountryDocumentId":"01800020146001001000001422021083019605712885","OtherData":null,"Messages":null,"ResponseValue":"","Code":99,"Description":"Error de validación.","ErrorException":"The element 'gEmis' in namespace 'http://ekuatia.set.gov.py/sifen/xsd' has incomplete content. List of possible elements expected: 'gActEco' in namespace 'http://ekuatia.set.gov.py/sifen/xsd'.","Folio":0}"

  • Suggested answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    Let me show you what I meant:

    {
    	"Success":false,
    	"GlobalDocumentId":"00000000-0000-0000-0000-000000000000",
    	"CountryDocumentId":"01800020146001001000001422021083019605712885",
    	"OtherData":null,
    	"Messages":null,
    	"ResponseValue":"",
    	"Code":99,
    	"Description":"Error de validación.",
    	"ErrorException":"The element 'gEmis' in namespace 'http://ekuatia.set.gov.py/sifen/xsd' has incomplete content. List of possible elements expected: 'gActEco' in namespace 'http://ekuatia.set.gov.py/sifen/xsd'.",
    	"Folio":0
    }

    Isn't this much easier to read than everything above?

    For simplicity, let me run this piece of code:

    str json = @'{
        "Success":false,
        "GlobalDocumentId":"00000000-0000-0000-0000-000000000000",
        "CountryDocumentId":"01800020146001001000001422021083019605712885",
        "OtherData":null,
        "Messages":null,
        "ResponseValue":"",
        "Code":99,
        "Description":"Error de validación.",
        "ErrorException":"",
        "Folio":0
        }';
    
    JSONContractEnvFac contract = FormJsonSerializer::deserializeObject(classNum(JSONContractEnvFac), json);

    This works - the object is successfully deserialized:

    pastedimage1664809917350v1.png

    But compare what I have in json variable (when using the text visualizer):

    pastedimage1664809980096v2.png

    Do you see the difference? Your code contains \" instead of ".

    This is the same data shows by the JSON visualizer:

    pastedimage1664810060313v3.png

    Please try it too - I think you'll get a meaningless result, because your text isn't valid JSON.

    By the way, my previous reply shouldn't be marked as the verified answer. It doesn't address your problem about "JSON Deserialization returning null Contract class", it's just just a request for getting the right input from you.

  • Osval25 Profile Picture
    20 on at

    Martin, I see that the code works for you, what do you think is my problem? The response form of the Rest API? Because it returns a text with \. How could I solve this so that it deserializes correctly? Thank you for helping me

  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    Make theories and test them.

    The current theory is that the backslashes in the string are causing the problem. You can test it by removing them before trying to deserialize the string (e.g. by strRem()). If it starts working, you'll know for sure that this was the problem.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 663 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 540 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 348 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans