最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - mapping cognito custom attribute JSON string to object - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Jan 20 at 2:55 user26912306user26912306 2
  • 1 Perhaps show your attempt and provide the error that you get? – Yong Shun Commented Jan 20 at 3:26
  • Also why in your UserDto use string instead of List<string> type? – Yong Shun Commented Jan 20 at 3:44
Add a comment  | 

1 Answer 1

Reset to default 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
});
发布评论

评论列表(0)

  1. 暂无评论