Deserialising a Json string throwing error in razor pages.

Blooming Developer 276 Reputation points
2023-01-17T03:35:29.02+00:00

Hi ,

I have a jsonresult like shown below

"{\r\n  "AccessRights": [],\r\n  "ADAttributes": [\r\n    {\r\n      \
"AD": "teenajohn",\r\n      \
"ADFull": "",\r\n      \
"DisplayName": "Teena John",\r\n      \
"FirstName": "Teena",\r\n      \
"Initials": "",\r\n      \
"EmployeeNumber": "1234567",\r\n      \
"LastName": "John",\r\n      \
"EmailAddress": "Teena.John@gmail.com",\r\n      \
"Department": "",\r\n      \
"Title": "",\r\n      \
"BusinessPhone": "+65 0000000",\r\n      \
"BusinessPhone2": "0000000",\r\n      \
"Mobile": "",\r\n      \
"CleanMobile": "",\r\n      \
"Company": "",\r\n      \
"Office": "Place1",\r\n      \
"Address": "Place2 ",\r\n      \
"City": "Place 3",\r\n      \
"State": "Place 4",\r\n      \
"PostalCode": "111111",\r\n      \
"Country": "",\r\n      \
"CountryCode": "",\r\n      \
"CountryCodeISO": "",\r\n      \
"Region": "ASIA",\r\n      \
"Building": "",\r\n      \
"BuildingAbbr": "",\r\n      \
"CountryCodeUN": "",\r\n      \
"Manager": "",\r\n      \
"Photo": "",\r\n      \
"SitePlantID": ,\r\n      \
"DefaultSite": "",\r\n      \
"DefaultPlant": "",\r\n      \
"PreGreet": "",\r\n      \
"PostGreet": "",\r\n      \
"NickName": "",\r\n      \
"AdditionalMessage": "",\r\n      \
"Okta": "Teena.John@gmail.com"\r\n    }\r\n  
],
\r\n  "OktaAttributes": [\r\n    {\r\n      "Okta": "Teena.John@gmail.com",\r\n      "ID": "",\r\n      "AD": "teenajohn",\r\n      "EmployeeNumber": "",\r\n      "Status": "ACTIVE",\r\n      "CreatedDate": "2022-11-15T12:27:33",\r\n      "ActivatedDate": "2022-11-15T12:27:33",\r\n      "StatusChangedDate": "2022-11-15T12:27:33",\r\n      "LastLogin": "2023-01-16T09:38:48",\r\n      "LastUpdatedDate": "2023-01-16T09:38:48",\r\n      "FirstName": "Teena",\r\n      "LastName": "John",\r\n      "SiteCode": "",\r\n      "Email": "Teena.John@gmail.com",\r\n      "SecondEmail": "",\r\n      "ManagerAD": "",\r\n      "ManagerEmail": "",\r\n      "HRAdvisor": "",\r\n      "Title": "",\r\n      "RockFunction": "",\r\n      "Mobile": "",\r\n      "CountryCode": "",\r\n      "UserType": "AD User",\r\n      "ProviderType": "ACTIVE_DIRECTORY",\r\n      "ProviderName": "asia.ad.flextronics.com"\r\n    }\r\n  ],\r\n  "IsPublic": false,\r\n  "AuthenticateType": 1\r\n}"

I want to get the AD Attributes from this json result like AD, DisplayName, FirstName all those informations.

For that i created a Model Class Like this,

 public class AuthenticateUserInfo
    {
        public Dictionary<string, ADUserInfo>? ADAttributes { get; set; }
        public Dictionary<string, OktaUserInfo>? OktaAttributes { get; set; }

    }

    public class ADUserInfo
    {
        public string AD { get; set; }
        public string ADFull { get; set; }      
        public string DisplayName { get; set; }
        public string FirstName { get; set; }
        public string Initials { get; set; }
        public string EmployeeNumber { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
        public string Department { get; set; }
        public string Title { get; set; }
        public string Manager { get; set; }
    }

    public class OktaUserInfo
    {

    }
}

And my login.cshtml.cs file i am trying to fetch those details like this,

if (result.Success)
                {
                    Console.WriteLine("Success");
                    string jsonString = result.JsonResult.ToString();
                    AuthenticateUserInfo AuthenticateUserInfo = JsonSerializer.Deserialize<AuthenticateUserInfo>(jsonString);
                    Console.WriteLine(AuthenticateUserInfo.ADAttributes);
                    return Redirect("/Privacy");
                    
                    

                }

But it is throwing error, Any one could advice where i am going wrong?

Thanks,

Teena John

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
696 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,140 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. PatriceSc 166 Reputation points
    2023-02-01T17:07:55.4633333+00:00

    Hi,

    The error message could help. Assuming this is what you see in a debugger, I replaced \r\n and <CRLF> to get the real value (always show that IMO) and saw using a validation tool that you have :

    "SitePlantID": ,

    ie the value is entirely missing and the json you get is not valid for some reason. Try to see if it could be fixed on the other side.

    0 comments No comments

  2. PatriceSc 166 Reputation points
    2023-02-01T17:18:39.4933333+00:00

    (posted twice as the previosu was first not showing up ?)

    0 comments No comments

  3. Bruce (SqlWork.com) 55,041 Reputation points
    2023-02-01T20:09:52.1+00:00

    assuming you fix the invalid json, change the root class to:

        public class AuthenticateUserInfo
        {
            public ADUserInfo[]? ADAttributes { get; set; }
            public OktaUserInfo[]? OktaAttributes { get; set; }
        }
    
    0 comments No comments

  4. Bruce (SqlWork.com) 55,041 Reputation points
    2023-02-01T20:11:29.9466667+00:00

    (extra post to get first to appear)

    0 comments No comments