0

Does the ChoJSONWriter or Newtonsoft support apply custom formatting to each JSON property. After pulling my data from the datasource I would like apply the following format to each JSON record.

{
  "Place": "{0}",
  "SkuNumber": "SKU_{1}"
}

I can do it manually by going through each record and applying String.Format but I dont want to reinvent the wheel.

Raihan Iqbal
  • 407
  • 3
  • 16
  • can you pls tell us what approach (dynamic / POCO) you are using to create JSON file? sample code helps. – Cinchoo Jul 02 '18 at 13:57

1 Answers1

0

This is how you can add custom format to each member using Cinchoo ETL

Dynamic Approach:

StringBuilder sb = new StringBuilder();

using (var w = new ChoJSONWriter(sb)
    .WithField("Place")
    .WithField("SkuNumber", valueConverter: (o) => String.Format("SKU_{0}", o.ToNString()))
    )
{
    dynamic o1 = new ExpandoObject();
    o1.Place = 1;
    o1.SkuNumber = 100;

    w.Write(o1);
}

Console.WriteLine(sb.ToString());

POCO:

public class PlaceObj
{
    public string Place { get; set; }
    public int SkuNumber { get; set; }
}

POCO Approach:

StringBuilder sb = new StringBuilder();

using (var w = new ChoJSONWriter<PlaceObj>(sb)
    .WithField(m => m.SkuNumber, valueConverter: (o) => String.Format("SKU_{0}", o.ToNString()))
)
{
    PlaceObj o1 = new PlaceObj();
    o1.Place = "1";
    o1.SkuNumber = 100;

    w.Write(o1);
}

Console.WriteLine(sb.ToString());
Cinchoo
  • 6,088
  • 2
  • 19
  • 34
  • Thanks for the answer but the solution needs to be generic as I do not know what fields will be returned and the list of fields are available only at runtime because the DataSource is a CSV file. – Raihan Iqbal Jul 03 '18 at 01:05
  • Can u post a sample code with input and expected output? – Cinchoo Jul 03 '18 at 02:20