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 Answer
Reset to default 0Concern:
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.
- 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
});
UserDto
usestring
instead ofList<string>
type? – Yong Shun Commented Jan 20 at 3:44