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 :
Small and medium business | Business Central, N...
Answered

C# Extract E-tag from json

(0) ShareShare
ReportReport
Posted on by 87

How would one extract the e-tag safely from the json returned for a list of entities like vendor?  How are you guys getting the e-tag value out of the json with C#?

I have the same question (0)
  • Suggested answer
    Stefano Demiliani Profile Picture
    37,166 Most Valuable Professional on at

    Nort clear the question. Normally you should simply parse the JSON response and retrieve the "@odata.etag" token from that JSON.

  • HPC Keith Profile Picture
    87 on at

    Yes, sorry about not being clear.  I would like to see how others are parsing the json to retrieve the @odata.etag token because you can't create a C# object class property with special characters, so I assume there is some manual parsing instead of using the automatic mapping.  I have generic Http method functions so that I can reuse them to make the BC API calls.  One of them is the Get:

     private async Task<JObject> GetAsync(string url)
    {
    try
    {
    var response = await _client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
    response.EnsureSuccessStatusCode();

    var data = await response.Content.ReadAsStringAsync();
    var jobj = JObject.Parse(data);

    return jobj;
    }
    catch (Exception ex)
    {
    throw ex;
    }
    }

    I then call that method from a specific GetVendors function:

     public async Task<Vendor> GetVendorByName(string name)
     {
       Vendor vendor = null;  

        try
        {
            var url = $"{_client.BaseAddress}/companies({CompanyId})/vendors?$filter=displayName eq '{CleanFilter(name)}'";
            var jobj = await GetAsync(url); // since it was filtered, an array of vendors are returned (even though it's just 1 in the array)
            var token = jobj.SelectToken("value");

            if (token != null)
            {
                var vendors = token.ToObject<List<Vendor>>(); // automatically maps the json (using the standard serializer) to a list of Vendor.  I would like to serialize the etag here somehow

                vendor = vendors.FirstOrDefault();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return vendor;
     }

    Now, when I get a vendor by Id, the API returns a single object with the etag easily available, but not easily serialized from the json using the standard serlializer:

     public async Task<Vendor> GetVendor(Guid id)
    {
    Vendor vendor = null;

    try
    {
    var url = $"{_client.BaseAddress}/companies({CompanyId})/vendors({id})";
    var jobj = await GetAsync(url);
    var etag = jobj.Value<string>("@odata.etag");

    vendor = jobj.ToObject<Vendor>();
    vendor.ETag = etag;
    }
    catch (Exception ex)
    {
    throw ex;
    }

    return vendor;
    }

    Not sure if that's clear or not, but I'm trying to automatically serialize the json into a Vendor class and include the e-tag but I do not know how.

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,166 Most Valuable Professional on at

    You can create a Vendor class like:

    public class Vendor
    {
        [JsonProperty("@odata.etag")]
        public string etag { get; set; }
        public string YourField1 { get; set; }
        //other properties here
    }

    and then deserialize with:
    var value = JsonConvert.DeserializeObject<Vendor>(json);
     
  • HPC Keith Profile Picture
    87 on at

    Wow, now don't I feel goofy.  Here I was looking at advanced extraction methods for that particular tag in the json when all I had to do was mark up the class.  Thank you so much, this is the perfect solution and I am ashamed I didn't think of it.

    pastedimage1594156946884v1.png

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 > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,229

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,867 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,153 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans