Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to AutoMapper and I wanted to use this to map nested API Json response to its Model class. Currently I am doing this manually with a Model class method where I am assigning the properties to its parameters.

//Here is the Json
{
  "id": "bfdb6f2f-91bb-4765-80ac-03649a6c82fe",
  "name": "Its a Name",
  "description": "Description 12345",
  "setId": "bfdb6f2f-91bb-4765-80ac-03649a6c82fe",
  "lastOpened": "2020",
  "startNode": {
    "isAutomatic": true,
    "definition": {
      "valueType": 1,
      "useExists": false,
      "operator": 0,
      "values": [
        {
          "valueType": 1,
          "useExists": false,
          "operator": 0,
          "values": [
            {
              "id": "DataType",
              "operator": 0,
              "valueType": 0,
              "value": [
                10001
              ]
            }
          ]
        }
      ]
    },
    "trueNode": {
      "type": 1,
      "classificationId": "9b622645-487c-445c-8129-5da66b9e7ecb",
      "replaceClassificationModeRule": 0,
      "sendToAgent": true,
      "nextNode": {
        "type": 8,
        "message": "Something",
        "nextNode": {
          "type": 9,
          "id": "7389e49b-9a55-4b5c-9cd8-8ac69a4b334d"
        },
        "id": "fa9f3eaf-e12b-4310-8948-781873f3e35d"
      },
      "id": "2154f6c1-8f8b-4f7f-90f1-f3dbb5d7a96f",
      "isAutomatic": false
    },
    "falseNode": {
      "type": 3,
      "resultAction": 0,
      "nextNode": {
        "type": 9,
        "id": "9a283d63-e0e2-429b-b053-a656e80ad163"
      },
      "id": "80e06635-e515-4f5d-a3f6-1aeb1750ace8",
      "isAutomatic": false
    },
    "type": 0,
    "weight": 50,
    "id": "c9b0e6a5-bbad-49db-a6f5-8849456d4020",
    "title": "Auto"
  },
  "quarantinePaths": null,
  "playbookDataTypes": [
    {
      "settings": null,
      "id": "8108cf4b-8288-4344-bcfa-00bab1a6fe41",
      "title": "SSN",
      "typeId": 10001,
      "isCustomType": false,
      "icon": null,
      "iconDisplay": null,
      "weight": 1
    }
  ]
}



//Model class of the API response

public JsonModelResponse 
{
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public Guid SetId { get; set; }
        public DateTimeOffset LastOpened { get; set; }
        public StartNode StartNode { get; set; }
        public object QuarantinePaths { get; set; }
        public List<PlaybookDataType> PlaybookDataTypes { get; set; }
    

    public class PlaybookDataType
    {
        public object Settings { get; set; }
        public Guid Id { get; set; }
        public string Title { get; set; }
        public long TypeId { get; set; }
        public bool IsCustomType { get; set; }
        public object Icon { get; set; }
        public object IconDisplay { get; set; }
        public long Weight { get; set; }
    }

    public class StartNode
    {
        public bool IsAutomatic { get; set; }
        public Definition Definition { get; set; }
        public TrueNode TrueNode { get; set; }
        public FalseNode FalseNode { get; set; }
        public long Type { get; set; }
        public long Weight { get; set; }
        public Guid Id { get; set; }
        public string Title { get; set; }
    }

    public class Definition
    {
        public long ValueType { get; set; }
        public bool UseExists { get; set; }
        public long Operator { get; set; }
        public List<DefinitionValue> Values { get; set; }
    }

    public  class DefinitionValue
    {
        public long ValueType { get; set; }
        public bool UseExists { get; set; }
        public long Operator { get; set; }
        public List<ValueValue> Values { get; set; }
    }

    public class ValueValue
    {
        public string Id { get; set; }
        public long Operator { get; set; }
        public long ValueType { get; set; }
        public List<long> Value { get; set; }
    }

    public class FalseNode
    {
        public long Type { get; set; }
        public long ResultAction { get; set; }
        public FalseNodeNextNode NextNode { get; set; }
        public Guid Id { get; set; }
        public bool IsAutomatic { get; set; }
    }

    public class FalseNodeNextNode
    {
        public long Type { get; set; }
        public Guid Id { get; set; }
    }

    public class TrueNode
    {
        public long Type { get; set; }
        public Guid ClassificationId { get; set; }
        public long ReplaceClassificationModeRule { get; set; }
        public bool SendToAgent { get; set; }
        public TrueNodeNextNode NextNode { get; set; }
        public Guid Id { get; set; }
        public bool IsAutomatic { get; set; }
    }

    public class TrueNodeNextNode
    {
        public long Type { get; set; }
        public string Message { get; set; }
        public FalseNodeNextNode NextNode { get; set; }
        public Guid Id { get; set; }
    }
}


What I have tried:

Not sure how to use for nested json response..
Posted
Updated 20-Jul-20 6:03am

1 solution

I would suggest to start from some online documentation to learn and try. You can start with one of the articles here at CodeProject: AutoMapper[^]

For nested, you would try something like:
C#
Mapper.CreateMap<Customer, CustomerViewItem>()
    .ForMember(cv => cv.FullName, m => m.MapFrom(
    s => s.FirstName + " " + s.LastName))

Right from their own documentation: Nested Mappings — AutoMapper documentation[^]

Happy Learning!
 
Share this answer
 
Comments
Member 14779968 20-Jul-20 12:12pm    
Thank you so much for providing the documentation. :)
Sandeep Mewara 20-Jul-20 12:23pm    
Sure!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900