Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert key value from result to uppercase in an ASP.NET Core 3.1 Web API

Model class:

public class User
{
    public string NS_ID { get; set; }
    public string TONV_ABC { get; set; }
    public string NAME_ABC { get; set; }
    public string MAABC { get; set; }
}

Result json when testing in Postman:

    "nS_ID": "",
    "tonV_ABC": "",
    "namE_ABC": "",
    "maabc": "F02",

This is the result I want:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    "NS_ID": "",
    "TONV_ABC": "",
    "NAME_ABC": "",
    "MAABC": "F02",

This is my API:

    [HttpGet]
    [Route("test")]
    public ActionResult test()
    {
        User u = new User();
        u.NS_ID = "";
        u.TONV_ABC = "";
        u.NAME_ABC = "";
        u.MAABC = "F02";
        return Ok(u);
    }

Please help me

I try convert but it’s not working

>Solution :

you can try to deserialize the JSON string to a Dictionary<string, object>. then create another Dictionary. iterate over the first dictionary and add all the key-value pairs to the new dictionary why making the key to upper case using the ToUpper() method. then serialize the new dictionary to the JSON string.

Sample code:

Dictionary<string, object> jsonDictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
Dictionary<string, object> result = new Dictionary<string, object>();
foreach (KeyValuePair<string, object> kvp in jsonDictionary)
{
    result.Add(kvp.Key.ToUpper(), kvp.Value);
}
string resultJson = JsonConvert.SerializeObject(result);

Note: I have used NewtonSoft.json. you can get it from the Nuget package manager.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading