-1

i have one aws cognito custom attribute called custom:events_permission and it will be something like this

[
   {
      "id":"48d258b7-8949-41z4-815a-f141487a6de1",
      "name":"event name",
      "user_id":"4eef6471-9a79-40r1-qw95-f22974f492d6",
      "role":"exhibitor-admin",
      "exhibitor_account_id":"42e812fe-f7ac-48fe-8a15-a0cb80c3ab59",
      "permissions":[
         "exh-manual:['view','edit','add']",
         "exh-leads:['*']",
         "exh-usr:['*']"
      ]
   }
]

and i have a DTO object called UserDto:

public class UserDto
{
    public string ExhibitorId { get; set; }
    public string UserId { get; set; }
    public string Type { get; set; }
    public string TenantId { get; set; }
    public string Role { get; set; }
    public string Permissions { get; set; }
}

how can i use automapper to map this, ExhibitorId to the exhibitor_account_id and Permissions to permissions and so on.

2
  • 1
    Perhaps show your attempt and provide the error that you get? Commented Jan 20 at 3:26
  • Also why in your UserDto use string instead of List<string> type? Commented Jan 20 at 3:44

1 Answer 1

0

Concern: Based on the attached JSON and UserDto, the Permissions should use the List<string> type instead of string. Unless the reason you use the string type is that you want to return the serialized JSON array for Permissions.


If you are extracting the data from the JSON (deserialization), you can work with JSON libraries such as System.Text.Json, Newtonsoft.Json, etc without AutoMapper.

For my example, I am using the System.Text.Json library.

  1. Define the JSON property field name to be mapped (if the field names are different)
public class UserDto
{
    [JsonPropertyName("exhibitor_account_id")]
    public string ExhibitorId { get; set; }
    public string UserId { get; set; }
    public string Type { get; set; }
    public string TenantId { get; set; }
    public string Role { get; set; }
    public List<string> Permissions { get; set; }
}

Since the attached JSON is with the snake case property name, you should apply JsonNamingPolicy.SnakeCaseLower

List<UserDto> users = JsonSerializer.Deserialize<List<UserDto>>(/* JSON string value */, new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
});
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.