1

I have an Azure Function V4. It is a Http triggered function returning IActionResult. My expectation of the output JSON should be property names matching exact letter casing of property names of the C# class. but the JSON serialization converts property names to camel case and lower case.

I want the JSON object property names letter casing to exactly match that of the C# class member names of that model.

C# Object

public string JobId { get; set; }
public List<Contact> Contacts { get; set; }
public Tags Tags { get; set; }
    

public class Contact
{
    public string firstName { get; set; }
}
    
public class Tags
{
    public string GROUPCONTACT { get; set; }
}

JSON result Actual

{
    "jobId": null,
    "contacts": [
        {
            "firstName": "dummy"
        }
    ],
    "tags": {
        "groupcontact": "dummy"
    }
}

Expected JSON result

{
    "JobId": null,
    "Contacts": [
        {
            "firstName": "dummy"
        }
    ],
    "Tags": {
        "GROUPCONTACT": "dummy"
    }
}
blogs4t
  • 2,329
  • 5
  • 20
  • 33
  • 1
    Does [Specifying JsonSerializerOptions in .NET 6 isolated Azure Function](https://stackoverflow.com/a/74427929/3744182) answer your question? You would need `options.PropertyNamingPolicy = null` rather than `JsonNamingPolicy.CamelCase`. If not, could you show how you are returning your `IActionResult`? – dbc Dec 02 '22 at 20:13
  • I hope you don't mind but I have edited your post's title to include a question I think is relevant for the body. Good luck! –  Dec 02 '22 at 22:40

1 Answers1

0

As @dbc mentioned in the comments, you have to specify PropertyNamingPolicy = null instead of PropertyNamingPolicy = JsonNamingPolicy.CamelCase to keep the property names as unchanged.

var serializeOptions = new JsonSerializerOptions 
{
 PropertyNamingPolicy = null 
};

If you are using camel case property naming policy (PropertyNamingPolicy = JsonNamingPolicy.CamelCase), you can use[JsonPropertyName] attributes to override the property names as shown below:


    public class Contact
    {
        public string firstName { get; set; }
    }
        
    public class Tags
    {
        [JsonPropertyName(Details)]
        public string GROUPCONTACT { get; set; }
    }

Output:


    {
        "contacts": [
            {
                "firstName": "XXXXX"
            }
        ],
        "tags": {
            "Details": "XXXXX"
        }
    }

Pravallika KV
  • 2,415
  • 2
  • 2
  • 7