Unable parse Json

Pradyut Sinha 21 Reputation points
2023-01-26T19:22:34.45+00:00

Can any body help me parse the below json in ASP.net C#

i need the grantedTo": email and Id also.

I have tried many ways but unable to parse.

Can anybody help.

{"@odata.context":https://graph.microsoft.com/v1.0/$metadata#users('31964c3b-f2ed-455d-8dc7-5b12e8c6be46')/drive/items('01FIJ7336ZHF5XAVMMUFCYDXFHXZRICATY')/permissions,"value":[{"id":"aTowIy5mfG1lbWJlcnNoaXB8YWJhbmVyNjJAaXRzLmpuai5jb20","roles":["read"],"grantedToV2":{"user":{"@odata.type":"#microsoft.graph.sharePointIdentity","displayName":"Banerjee, Alapan [JJCUS NON-J&J]","email":ABaner62@ITS.JNJ.com,"id":"09748e1f-07c7-4b76-b239-2842882b9294"},"siteUser":{"displayName":"Banerjee, Alapan [JJCUS NON-J&J]","email":ABaner62@ITS.JNJ.com,"id":"21","loginName":"i:0#.f|membership|abaner62@its.jnj.com"}},"grantedTo":{"user":{"displayName":"Banerjee, Alapan [JJCUS NON-J&J]","email":ABaner62@ITS.JNJ.com,"id":"09748e1f-07c7-4b76-b239-2842882b9294"}}},{"id":"aTowIy5mfG1lbWJlcnNoaXB8YXlvdXN1ZkBpdHMuam5qLmNvbQ","roles":["read"],"grantedToV2":{"user":{"@odata.type":"#microsoft.graph.sharePointIdentity","displayName":"Yousuf, Adnan [JJCUS NON-J&J]","email":AYousuf@ITS.JNJ.com,"id":"b4c566a9-610e-41cc-be33-0aa7ca15cfcb"},"siteUser":{"displayName":"Yousuf, Adnan [JJCUS NON-J&J]","email":AYousuf@ITS.JNJ.com,"id":"46","loginName":"i:0#.f|membership|ayousuf@its.jnj.com"}},"grantedTo":{"user":{"displayName":"Yousuf, Adnan [JJCUS NON-J&J]","email":AYousuf@ITS.JNJ.com,"id":"b4c566a9-610e-41cc-be33-0aa7ca15cfcb"}}},{"id":"aTowIy5mfG1lbWJlcnNoaXB8c2EtcHJkLW9kZGV2Mi1hZG1pbkBpdHMuam5qLmNvbQ","roles":["owner"],"grantedToV2":{"user":{"@odata.type":"#microsoft.graph.sharePointIdentity","displayName":"SA-PRD-oddev2-admin","email":SA-PRD-oddev2-admin@its.jnj.com,"id":"31964c3b-f2ed-455d-8dc7-5b12e8c6be46"},"siteUser":{"displayName":"SA-PRD-oddev2-admin","email":SA-PRD-oddev2-admin@its.jnj.com,"id":"3","loginName":"i:0#.f|membership|sa-prd-oddev2-admin@its.jnj.com"}},"grantedTo":{"user":{"displayName":"SA-PRD-oddev2-admin","email":SA-PRD-oddev2-admin@its.jnj.com,"id":"31964c3b-f2ed-455d-8dc7-5b12e8c6be46"}}}]}

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,386 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 59,546 Reputation points
    2023-01-26T21:22:12.5033333+00:00

    first off your json is invalid. if you fix it and paste as json classes you get:

    public class Rootobject
    {
        public string odatacontext { get; set; }
        public Value[] value { get; set; }
    }
    
    public class Value
    {
        public string id { get; set; }
        public string[] roles { get; set; }
        public Grantedtov2 grantedToV2 { get; set; }
        public Grantedto grantedTo { get; set; }
    }
    
    public class Grantedtov2
    {
        public User user { get; set; }
        public Siteuser siteUser { get; set; }
    }
    
    public class User
    {
        public string odatatype { get; set; }
        public string displayName { get; set; }
        public string email { get; set; }
        public string id { get; set; }
    }
    
    public class Siteuser
    {
        public string displayName { get; set; }
        public string email { get; set; }
        public string id { get; set; }
        public string loginName { get; set; }
    }
    
    public class Grantedto
    {
        public User1 user { get; set; }
    }
    
    public class User1
    {
        public string displayName { get; set; }
        public string email { get; set; }
        public string id { get; set; }
    }
    

    then its a simple foreach:

    var model = JsonSerializer.Deserialize<Rootobject>(jsonString);
    foreach (var value in model.value)
    {
        var id = value.id;
        var grantedTo = value.grantedTo;
    }
    

  2. QiYou-MSFT 4,311 Reputation points Microsoft Vendor
    2023-01-27T05:53:53.95+00:00

    Hi @Pradyut Sinha

    My thoughts are:

    1. Declare classes and lists.
    2. Assign a value to the data.
    3. Generate JSON variables.
    4. Print it out

    For demonstration purposes, I printed the JSON data as string data.

    In order to keep the same format as JSON, I set an option.

    var options = new JsonSerializerOptions { WriteIndented = true };

    string jsonString = System.Text.Json.JsonSerializer.Serialize(outputdata, options);

    Of course, you can also print it directly.

    You can refer to the following code:

    public class Information
    {
        public string atodatadotcontext { get; set; }
        public Value[] value { get; set; }
    }
    public class Value
    {
        public string id { get; set; }
        public string role { get; set; }
        public GrantedTov2 grantedToV2 { get; set; }
        public GrantedTo grantedTo { get; set; }
    }
    public class GrantedTov2
    {
        public User user { get; set; }
        public SiteUser siteUser { get; set; }
    }
    public class User
    {
        public string atodatadottype { get; set; }
        public string displayName { get; set; }
        public string email { get; set; }
        public string id { get; set; }
    }
    public class SiteUser
    {
        public string displayName { get; set; }
        public string email { get; set; }
        public string id { get; set; }
        public string loginName { get; set; }
    }
    public class GrantedTo
    {
        public User1 user1 { get; set; }
    }
    public class User1
    {
        public string displayName { get; set; }
        public string email { get; set; }
        public string id { get; set; }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            var user1_1 = new User1
            {
                displayName = "Banerjee,Alapan[JJCUS NON - J & J]",
                email= "ABaner62@ITS.JNJ.com",
                id= "09748e1f-07c7-4b76-b239-2842882b9294"
            };
            var user1_2 = new User1
            {
                displayName = "Yousuf, Adnan [JJCUS NON-J&J]",
                email= "AYousuf@ITS.JNJ.com",
                id= "b4c566a9-610e-41cc-be33-0aa7ca15cfcb"
            };
            var user1_3 = new User1
            {
                displayName = "SA-PRD-oddev2-admin",
                email = "SA-PRD-oddev2-admin@its.jnj.com",
                id = "31964c3b-f2ed-455d-8dc7-5b12e8c6be46"
            };
            var user1 = new User
            {
                atodatadottype = "#microsoft.graph.sharePointIdentity",
                displayName = "Banerjee, Alapan [JJCUS NON-J&J]",
                email = "ABaner62@ITS.JNJ.com",
                id = "09748e1f-07c7-4b76-b239-2842882b9294"
            };
            var user2 = new User
            {
                atodatadottype = "#microsoft.graph.sharePointIdentity",
                displayName = "Yousuf, Adnan [JJCUS NON-J&J]",
                email = "AYousuf@ITS.JNJ.com",
                id = "b4c566a9-610e-41cc-be33-0aa7ca15cfcb"
            };
            var user3 = new User
            {
                atodatadottype = "#microsoft.graph.sharePointIdentity",
                displayName = "SA-PRD-oddev2-admin",
                email = "SA-PRD-oddev2-admin@its.jnj.com",
                id = "31964c3b-f2ed-455d-8dc7-5b12e8c6be46"
            };
            var siteuser1 = new SiteUser
            {
                displayName= "Banerjee, Alapan [JJCUS NON-J&J]",
                email = "ABaner62@ITS.JNJ.com",
                id="21",
                loginName= "i:0#.f|membership|abaner62@its.jnj.com"
            };
            var siteuser2 = new SiteUser
            {
                displayName = "Yousuf, Adnan [JJCUS NON-J&J]",
                email = "AYousuf@ITS.JNJ.com",
                id = "46",
                loginName = "i:0#.f|membership|ayousuf@its.jnj.com"
            };
            var siteuser3 = new SiteUser
            {
                displayName = "SA-PRD-oddev2-admin",
                email = "SA-PRD-oddev2-admin@its.jnj.com",
                id = "3",
                loginName = "i:0#.f|membership|sa-prd-oddev2-admin@its.jnj.com"
            };
            var grantedto1 = new GrantedTo
            {
                user1 = user1_1
            }; 
            var grantedto2 = new GrantedTo
            {
                user1 = user1_2
            };
            var grantedto3 = new GrantedTo
            {
                user1 = user1_3
            };
            var grantedtov2_1 = new GrantedTov2
            {
                user=user1,
                siteUser=siteuser1
            };
            var grantedtov2_2 = new GrantedTov2
            {
                user = user2,
                siteUser = siteuser2
            };
            var grantedtov2_3 = new GrantedTov2
            {
                user = user3,
                siteUser = siteuser3
            };
            Value[] value1 = new Value[3];
            value1[0] = new Value
            {
                id = "aTowIy5mfG1lbWJlcnNoaXB8YWJhbmVyNjJAaXRzLmpuai5jb20",
                role = "[read]",
                grantedToV2 = grantedtov2_1,
                grantedTo=grantedto1
            };
            value1[1] = new Value
            {
                id = "aTowIy5mfG1lbWJlcnNoaXB8YXlvdXN1ZkBpdHMuam5qLmNvbQ",
                role = "[owner]",
                grantedToV2 = grantedtov2_2,
                grantedTo = grantedto2
            };
            value1[2] = new Value
            {
                id = "aTowIy5mfG1lbWJlcnNoaXB8c2EtcHJkLW9kZGV2Mi1hZG1pbkBpdHMuam5qLmNvbQ",
                role = "[owner]",
                grantedToV2 = grantedtov2_2,
                grantedTo = grantedto2
            };
            var inforamtion = new Information
            {
                atodatadotcontext = "https://graph.microsoft.com/v1.0/$metadata#users('31964c3b-f2ed-455d-8dc7-5b12e8c6be46')/drive/items('01FIJ7336ZHF5XAVMMUFCYDXFHXZRICATY')/permissions",
                value=value1
            };
            var options = new JsonSerializerOptions { WriteIndented = true };
            string jsonString = JsonSerializer.Serialize(inforamtion, options);
            Console.WriteLine(jsonString);
            Console.Read();
        }
    }
    

    Result:Case13

    Best Regards

    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. QiYou-MSFT 4,311 Reputation points Microsoft Vendor
    2023-02-03T08:43:33.0966667+00:00

    Hi @Pradyut Sinha

    I'm sorry I misunderstood you before.

    First of all, judging from the JSON data you gave, this should be taken from the API.

    Since I don't know which API your json got from. Then I don't know what the data structure of your JSON is. So I wrote a structure based on your information.

    And based on your comments, I'm afraid I misunderstood your requirement. Did you want to get the id and email attributes of grantedTo from json data? If this is the case, you could try this method:

    string jsonString1 = File.ReadAllText("C:\\Users\\Administrator\\Desktop\\ABC.JSON");    
                Information information1=JsonSerializer.Deserialize<Information>(jsonString1);
                
                
                for (int i = 0; i < information1.value.Length; i++)
                {
                    Console.WriteLine($"Id: {information1.value[i].grantedTo.user1.id}");
                    Console.WriteLine($"Email: {information1.value[0].grantedTo.user1.email}");
                }
    

    Information is the outermost class of this JSON

    jsonString1 is a json-type string converted from json.

    Testnew

    Best Regards

    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments