Search code examples
c#jsonblazordeserialization

How can I extract only selected properties/nodes from this json string?


I'm trying to deserialize this string to put the data in a table (I'm using Blazor), I only need the ItemSections' ItemIds (I need to put all of them in the table actually), the rest of it can be disposed of. Thing is I'm trying to make it work but I'm a bit stuck.

This is the string :

{
   "id":6,
   "description":"Accounting",
   "position":0,
   "enabled":true,
   "itemSections":[
      {
         "itemId":7,
         "sectionId":6,
         "item":{
            "id":7,
            "type":2,
            "description":"ctb_1",
            "notes":"",
            "shortDescription":"ctb_1",
            "code":"",
            "enabled":true,
            "guid":null,
            "itemSections":[
               null
            ],
            "layouts":[
               
            ],
            "scopes":[
               
            ],
            "users":[
               
            ],
            "bookmarks":[
               
            ],
            "tenantId":null
         },
         "section":null,
         "position":0
      },
      {
         "itemId":8,
         "sectionId":6,
         "item":{
            "id":8,
            "type":2,
            "description":"ctb_2",
            "notes":"",
            "shortDescription":"ctb_2",
            "code":"string",
            "enabled":true,
            "guid":null,
            "itemSections":[
               null
            ],
            "layouts":[
               
            ],
            "scopes":[
               
            ],
            "users":[
               
            ],
            "bookmarks":[
               
            ],
            "tenantId":null
         },
         "section":null,
         "position":0
      },
      {
         "itemId":9,
         "sectionId":6,
         "item":{
            "id":9,
            "type":2,
            "description":"TIMELINE ADEMPIMENTI",
            "notes":"s",
            "shortDescription":"TIMELINE ADEMPIMENTI",
            "code":"string",
            "enabled":true,
            "guid":null,
            "itemSections":[
               null
            ],
            "layouts":[
               
            ],
            "scopes":[
               
            ],
            "users":[
               
            ],
            "bookmarks":[
               
            ],
            "tenantId":null
         },
         "section":null,
         "position":0
      }
   ],
   "users":[
      {
         "id":1,
         "name":"Administrator",
         "email":null,
         "role":1,
         "login":"string",
         "password":"string",
         "enabled":true,
         "canImpersonate":true,
         "theme":null,
         "layouts":[
            
         ],
         "userFilters":[
            
         ],
         "items":[
            
         ],
         "scopes":[
            
         ],
         "sections":[
            null
         ],
         "bookmarks":[
            
         ],
         "tenantId":null
      }
   ]
}

This is the deserializing, just in case it's useful (items is a GridList) :

result = await url.WithOAuthBearerToken(Token).GetStringAsync();
var jsonDocument = JsonDocument.Parse(result);
items = JsonSerializer.Deserialize<GridList>(jsonDocument.RootElement, new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true,
});

Also the table I'm trying where I'm trying to put all of the data, maybe there is something wrong here too :

<table class="table table-striped">
    <thead>
        <tr>
            @* headers *@
            <th>Id</th>
            <th>Description</th>
            <th>Position</th>
            <th>Enabled</th>
            <th>Item Sections</th>
            <th>Users</th>
            <th>Data</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in items)
        {
            @* dati *@
            <tr>
                <td>@item.Id</td>
                <td>@item.Description</td>
                <td>@item.Position</td>
                <td>@item.Enabled</td>
                <td>@string.Join(", ", item.ItemSections)</td>
                <td>@string.Join(", ", item.Users)</td>
                <td>
                <button class="btn btn-secondary" @onclick="(e => GetId(item.Id)) style="background: linear-gradient(to bottom, #0b2363, #3a0647);">Apri Pagina</button>
                </td>

            </tr>
        }
    </tbody>
</table>    

Here's the class I used (yes, the ItemIds I was talking about are not implemented because as I said I'm stuck) :

    public class GridList
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public int Position { get; set; }
        public bool Enabled { get; set; }
        public List<string> ItemSections { get; set; }
        public List<string> Users { get; set; }
    }

Solution

  • You can read the json as JsonElement and then extract the desired value by traversing to the field, Here is how,

    string json = File.ReadAllText("YouFullJson.json");
    var element = JsonSerializer.Deserialize<JsonElement>(json, new JsonSerializerOptions(JsonSerializerDefaults.Web));
    var sections = element.GetProperty("itemSections").Deserialize<JsonElement[]>();
    var ids = sections.Select(o => o.GetProperty("itemId").GetInt32());
    Console.WriteLine(string.Join(", ",ids));