Search code examples
c#jsonasp.net-corehttpclientresponse

How to extract one level from json response and display it separatedly as independent data - C# .net core


I'm working with .net core app, without frontend, so all of the code is on server.

here is server's response afterHttpClient call:

 {
        "productId": null,
        "email": "[email protected]",
        "position": null,
        "title": "[email protected]",
        "price": null,
        "product_metadata": 
        {
            "organization": 
            {
                "organizationId": "",
                "title": null
            },
            "sale_stores": 
            [
                {
                    "storeId": "1",
                    "title": null
                },
                {
                    "storeId": "2",
                    "title": null
                }
            ],
        }
    },

I'm calling external API to get data so I can't influence what response I will get after I call exteral server. But I can modify this respone on my server ofcourse.

And I would like to get rid of "product_metadata" level so I could create a response for client (which will join later) which could look like this:

  {
        "productId": null,
        "email": "[email protected]",
        "position": null,
        "title": "[email protected]",
        "price": null,
        "organization": 
        {
           "organizationId": "",
           "title": null
        },
        "sale_stores": 
        [
            {
                "storeId": "1",
                "title": null
            },
            {
                "storeId": "2",
                "title": null
            }
         ],
    },

Here's my code - please read comments :

public async Task<IEnumerable<ProductDto>> GetProducts(CancellationToken cancellationToken)
{

    var response = await _Client.Get<List<ProductDto>>(url, accessToken, cancellationToken);
    // Somewhere in this Get method call I'm deserializing data to my Dto 
    // return JsonConvert.DeserializeObject<T>(result); < - this happening in Get method

    // Here I need to modify this response to rid of that one level that I dont need 
    // Maybe I can loop results and move it to another Dto without `product_metadata` level but that's performance issue?
    
    return response;
}

Here's my ProductDto

public class ProductDto
{
    public string ProductId { get; set; }
    public string Email { get; set; }
    public string Position { get; set; }
    public string Title { get; set; }
    public string Price { get; set; }

    [JsonProperty("product_metadata")]
    public ProductMetadataDto ProductMetadata { get; set; }
}

Thanks guys

Cheers


Solution

  • You will not have any performance issue by mapping it to another DTO, go for it! :) It's somewhat of a best practice to map to your own models that you control and return from your API. Do something like this:

    public async Task<IEnumerable<MyProductDto>> GetProducts(CancellationToken cancellationToken)
    {
        var response = await _Client.Get<List<ProductDto>>(url, accessToken, cancellationToken);
        var mappedResponse = myMapper.Map(response);
        return mappedResponse;
    }
    

    If you have a bunch of products, like 10000 or more, you should really look into pagination :)