1

I have a requirement to create a dynamic JSON response based on master model class in C#.

GIVEN

    public class Person
    {
       public string Firstname {get; set;}
       public string Lastname {get; set;}
       public string Gender {get; set;}
    }
    var personObj = new Person {Firstname = "Foo", Lastname = "Bar", Gender = "M"};
    var responseJsonTemplate1 = "{\"Firstname\":\"\",\"Gender\":\"\"}";
    var responseJsonTemplate2 = "{\"Firstname\":\"\",\"Lastname\":\"\"}";

EXPECTATION

   var responseJson1 = "{\"Firstname\":\"Foo\",\"Gender\":\"Male\"}";
   var responseJson2 = "{\"Firstname\":\"Foo\",\"Lastname\":\"Bar\"}";

I want to generate JSON response from the personObj based on the template JSON string.

Is there any library to do such type of mapping?

Note:

  1. The template JSON will always follow the structure of Person class
  2. Since the response JSON will be dynamic and with any combinations of properties, we can't create a concrete class for each response JSON. It has to be dynamically generated.
Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
Pradeep
  • 1,108
  • 1
  • 15
  • 31
  • _"Since the response JSON will be dynamic and with any combinations of properties"_ how do they get selected? – Fildor Jul 07 '20 at 09:13
  • try NewtonSoft.JSON – Vivek Nuna Jul 07 '20 at 09:18
  • You're asking about dynamic serialization, *not* dynamic mapping. That's one problem. The other is finding out the property names contained in those JSON strings. Any JSON parser could handle this, as those JSON strings are essentially dictionaries. Why do that though? Why not pass the property names as an array? Are you trying to implement some specific protocol? Perhaps it's already implemented – Panagiotis Kanavos Jul 07 '20 at 09:42
  • The final response JSON should be formed based on the properties defined in the template JSON and the template JSON can have any combinations of properties but the property path will be consistent wrt original personObj – Pradeep Jul 07 '20 at 09:43
  • So, basically I am looking for a generic logic to fill in the values of properties in the template JSON from the personObj dynamically. I can do this easily with AutoMapper if I create concrete classes for each template but that is what I do not want to do. – Pradeep Jul 07 '20 at 09:50
  • Does this answer your question? [How to Convert JSON object to Custom C# object?](https://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object) – TECNO Jul 14 '20 at 10:06

1 Answers1

-1

A good way to use JSON in C# is with JSON.NET

Quick Starts & API Documentation from JSON.NET - Official site help you work with it.

    var personObj = new Person { Firstname = "Foo", Lastname = "Bar", Gender = "M" };
    string json = JsonConvert.SerializeObject(personObj);

    Console.WriteLine(json);
    //{"Firstname":"Foo","Lastname":"Bar","Gender":"M"}

    //and if you want the aother way :

    string jsonS = @"{'Firstname': 'Foo','Lastname':'Bar','Gender':'M'}";
    Person personObj2 = JsonConvert.DeserializeObject<Person>(jsonS);
Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
TECNO
  • 162
  • 2
  • 3
  • 15