2

when writing the CSV file it does not have the values red and small

Example:

imput jsonFile:

[
  {
    "id": 1,
    "name": "Mike",
    "features": {
      "colors": [
        "blue"
      ],
      "sizes": [
        "big"
      ]
    }
  },
  {
    "id": 1,
    "name": "Jose",
    "features": {
      "colors": [
        "blue",
        "red"
      ],
      "sizes": [
        "big",
        "small"
      ]
    }
  }
]

output csvFile:

id;name;features_colors_0;features_sizes_0
1;Mike;blue;big
1;Jose;blue;big

Code:

using ChoETL;
using System.IO;

namespace TestJsonToCsv
{
    class Program
    {
        static void Main(string[] args)
        {
            var jsonPath = @"C:\Users\xxx\Downloads\test_array.json";
            var csvPath = Path.ChangeExtension(jsonPath, "csv");

            var config = new ChoJSONRecordConfiguration();
            config.Encoding = System.Text.Encoding.UTF8;

            using (var r = new ChoJSONReader(jsonPath, config))
            {
                using (var w = new ChoCSVWriter(csvPath).WithFirstLineHeader())
                {
                    w.Write(r);
                }
            }
        }
    }
}

Library version:
ChoETL - 1.2.1.14
ChoETL.JSON - 1.2.1.14
issue reference - https://github.com/Cinchoo/ChoETL/issues/138

Cinchoo
  • 6,088
  • 2
  • 19
  • 34
Fellipe
  • 493
  • 5
  • 7

1 Answers1

1

You will have to instruct the writer to scan the nodes to discover the sizes using WithMaxScanRows

Here is the working sample

string json = @"
[
  {
    ""id"": 1,
    ""name"": ""Mike"",
    ""features"": {
      ""colors"": [
        ""blue""
      ],
      ""sizes"": [
        ""big""
      ]
    }
  },
  {
    ""id"": 1,
    ""name"": ""Jose"",
    ""features"": {
      ""colors"": [
        ""blue"",
        ""red""
      ],
      ""sizes"": [
        ""big"",
        ""small""
      ]
    }
  }
]";

StringBuilder csv = new StringBuilder();

using (var r = ChoJSONReader.LoadText(json))
{
    using (var w = new ChoCSVWriter(csv)
        .WithFirstLineHeader()
        .WithMaxScanRows(2)
        .ThrowAndStopOnMissingField(false)
        )
    {
        w.Write(r);
    }
}
Console.WriteLine(csv.ToString());

Output:

id,name,features_colors_0,features_colors_1,features_sizes_0,features_sizes_1
1,Mike,blue,,big,
1,Jose,blue,red,big,small
Fellipe
  • 493
  • 5
  • 7
Cinchoo
  • 6,088
  • 2
  • 19
  • 34
  • this approach does not record all objects. In your example you did not write the first object "name: Mike ' – Fellipe Apr 12 '21 at 15:56
  • pls use `.ThrowAndStopOnMissingField(false)` on writer to ignore the missing fields. Updated the code above. – Cinchoo Apr 12 '21 at 17:01