using System.Collections.Generic;
public static void Main()
var listing = t.CreateListingObject();
var serialisedListing = JsonConvert.SerializeObject(listing);
var deserialisedListing = JsonConvert.DeserializeObject<Listing>(serialisedListing);
dynamic flattenedListing = t.FlattenObject(deserialisedListing);
foreach(var item in flattenedListing) {
public Listing CreateListingObject() {
var descriptors = new List<Descriptor>();
var colourDescriptor = new Descriptor {
var sizeDescriptor = new Descriptor {
SearchableValue = "Large"
descriptors.Add(colourDescriptor);
descriptors.Add(sizeDescriptor);
Descriptors = descriptors
public dynamic FlattenObject(Listing listing) {
dynamic flattenedListing = new ExpandoObject();
flattenedListing.Id = listing.Id;
flattenedListing.Title = listing.Title;
foreach (Descriptor descriptor in listing.Descriptors) {
var formattedDescriptor = new ExpandoObject();
var descriptorName = descriptor.Name.ToLower();
AddProperty(flattenedListing, "d_k_" + descriptorName, descriptor.Value);
AddProperty(flattenedListing, "d_t_"+ descriptorName +"_searchable_value", descriptor.SearchableValue);
public void AddProperty(ExpandoObject expando, string propertyName, object propertyValue)
var expandoAsDictionary = expando as IDictionary<string, object>;
if (expandoAsDictionary.ContainsKey(propertyName)) {
expandoAsDictionary[propertyName] = propertyValue;
expandoAsDictionary.Add(propertyName, propertyValue);
public long Id { get; set; }
public string Title { get; set; }
public IEnumerable<Descriptor> Descriptors { get; set; }
public class Descriptor {
public string Name { get; set; }
public string Value { get; set; }
public string SearchableValue { get; set; }