0

Consider I have below values in the drop-down (dropDownList variable) and user selected values are in selectedDropDownValues list.

And DB api returns the list of customers (customerDBList variable).

Now the requirement is to build JSON from selected values as mentioned below -

var dropDownList = new[] { "Customer.Id", "Customer.FirstName", "Customer.LastName", "Customer.Address.AddressLine1", "Customer.Address.AddressLine2" };

var selectedDropDownValues = new[] { "Customer.Id", "Customer.FirstName", "Customer.Address.AddressLine1" };

var customerDBList = new List<Customer>(){
                new Customer {
                    Id=1,
                    FirstName="John",
                    LastName="Desouza",
                    Address=new Address{            
                                AddressLine1="1 Street",
                                AddressLine2="Linking Road"
                    }},        
                new Customer {
                    Id=2,
                    FirstName="Sam",
                    LastName="Lewis",
                    Address=new Address{            
                                AddressLine1="Fedral Highway",
                                AddressLine2="Louisville"
                    }                
                }};

Expected JSON output as -

[
  {
    "Customer": {
      "Id": 1,
      "FirstName": "John",
      "Address": {
        "AddressLine1": "1 Street"
      }
    }
  },
  {
    "Customer": {
      "Id": 2,
      "FirstName": "Sam",
      "Address": {
        "AddressLine1": "Fedral Highway"
      }
    }
  }
]
dbc
  • 104,963
  • 20
  • 228
  • 340
Sumit Deshpande
  • 2,155
  • 2
  • 20
  • 36

1 Answers1

1

As it happens, your selectedDropDownValues strings "Customer.Address.AddressLine1" are in JSONPath syntax, so you could convert your Customer objects to an intermediate JObject then prune unwanted values using JsonExtensions.RemoveAllExcept(this JObject obj, IEnumerable<string> paths) from this answer to How to perform partial object serialization providing "paths" using Newtonsoft JSON.NET.

Note that your desired JSON has an extra level of nesting { "Customer": { ... } } not present in your data model, so it will need to be manually inserted before filtering:

var rootName = "Customer";

var query = customerDBList
    // Convert to JObject
    .Select(c => JObject.FromObject(c))
    // Add additional level of object nesting { "Customer": { ... } } 
    .Select(o => new JObject( new JProperty(rootName, o)))
    // Remove all but selected properties.
    .Select(o => o.RemoveAllExcept(selectedDropDownValues));

var json = JsonConvert.SerializeObject(query, Formatting.Indented);

Working sample .Net fiddle here which shows the generated JSON to be, as required,

[
  {
    "Customer": {
      "Id": 1,
      "FirstName": "John",
      "Address": {
        "AddressLine1": "1 Street"
      }
    }
  },
  {
    "Customer": {
      "Id": 2,
      "FirstName": "Sam",
      "Address": {
        "AddressLine1": "Fedral Highway"
      }
    }
  }
]
dbc
  • 104,963
  • 20
  • 228
  • 340