Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging nested arrays to csv when arrays are empty #304

Open
praveena1728 opened this issue Nov 9, 2023 · 1 comment
Open

Merging nested arrays to csv when arrays are empty #304

praveena1728 opened this issue Nov 9, 2023 · 1 comment

Comments

@praveena1728
Copy link

praveena1728 commented Nov 9, 2023

I have data in json similar to the structure below:

[
{
        "incident": {
            "id": "120950002",
            "name": "Mosquito Fire"
        },
        "additionalClaimant": [
            {
                "claimantFirstName": "Olga",
                "claimantLastName": "Yachnik",

            },
           {
                "claimantFirstName": "Meera",
                "claimantLastName": "Lavina",

            },
        ]
},
{
        "incident": {
            "id": "120950002",
            "name": "Mosquito Fire"
        },
        "additionalClaimant": [],
},
]

The json contains an array called "AdditionalClaimant" which sometimes has value but also can be an empty array. I need to convert this to csv using ChoETL where the claimantFirstName values in the array should merge to one column ("Olga,Meera"). and this is my code

    //Use ChoETL to convert data to CSV
    using (var r = ChoJSONReader.LoadText(attorneyClaimData)
        .WithField("Fire", jsonPath: "$.incident.name", isArray: false)
        .WithField("FirstName", jsonPath: "$.additionalClaimant[*].claimantFirstName", defaultValue: "null")
    )
    {
        using (var w = new ChoCSVWriter("AttorneyClaims.csv")
            .WithFirstLineHeader()
            .UseNestedKeyFormat(false)
            .Configure(c => c.ArrayValueSeparator = ';')
            )
        {
            w.Write(r);
        }
    }

The problem arises when the AdditionalClaimant array is empty. I hit the error as below
"message": "Failed to write 'System.Object[]' value for 'FirstName' field."
Please can you help around how to navigate around this?

@Cinchoo
Copy link
Owner

Cinchoo commented Dec 19, 2023

Here is how you can convert your sample json to csv

        static void Issue304()
        {
            string json = @"[
  {
    ""incident"": {
      ""id"": ""120950002"",
      ""name"": ""Mosquito Fire""
    },
    ""additionalClaimant"": [
      {
        ""claimantFirstName"": ""Olga"",
        ""claimantLastName"": ""Yachnik""

      },
      {
        ""claimantFirstName"": ""Meera"",
        ""claimantLastName"": ""Lavina""

      }
    ]
  },
  {
    ""incident"": {
      ""id"": ""120950002"",
      ""name"": ""Mosquito Fire""
    },
    ""additionalClaimant"": []
  }
]";

            using (var r = ChoJSONReader.LoadText(json)
                .Configure(c => c.DefaultArrayHandling = false)
                .Configure(c => c.FlattenNode = true)
                .Configure(c => c.UseNestedKeyFormat = true)
                .Configure(c => c.FlattenByNodeName = "additionalClaimant")
                .Configure(c => c.NestedColumnSeparator = '.')
                )
            {
                using (var w = new ChoCSVWriter(Console.Out)
                    .WithFirstLineHeader()
                    )
                {
                    w.Write(r);
                }

            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants