question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSON to CSV conversion using CHOETL Some array value missing

See original GitHub issue

I am converting a JSON file to a CSV file. The JSON has multiple nested objects and large size. While converting, I am able to get all the values out of the JSON and into the CSV. However, array values are missing . I am using CHOETL library. the sample json is (original json is long and big size)

{
    "getUsers": [
        {
            "UserInformation": {
                "Id": 1111122,
                "firstName": "*****1",
                "UserType": {
                    "name": "CP"
                },
                "primaryState": "MA",
                "otherState": [
                    "MA",
                    "BA"
                ],
                "createdAt": null
            }
        },
        {
            "UserInformation": {
                "Id": 3333,
                "firstName": "*****3",
                "UserType": {
                    "name": "CPP"
                },
                "primaryState": "MPA",
                "otherState": [
                    "KL",
                    "TN",
                    "DL",
                    "AP",
                    "RJ"
                ],
                "createdAt": null
            }
        }
    ]
}

the ‘otherState’ array in first array is two but second array its four. its not showing is csv. the input json is long and nested hierarchy and mostly dynamic the code is

StringBuilder msg = new StringBuilder();

            using (var w = new ChoCSVWriter(msg)
                .WithFirstLineHeader()
                )
            {
                using (var r = new ChoJSONReader(@"E:/Temp/CSV/input/Data_Sample2.json")
                    .WithJSONPath("$..getUsers[*]")
                    )
                {
                    w.Write(r);
                }
            }
            File

the output is

UserInformation_Id,UserInformation_firstName,UserInformation_UserType_name,UserInformation_primaryState,UserInformation_otherState_0,UserInformation_otherState_1,UserInformation_createdAt

1111122,*****1,CP,MA,MA,BA,
3333,*****3,CPP,MPA,KL,TN,

i want each user in different row, when try with all data in single row its working fine

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:20 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
Cinchoocommented, Jan 1, 2020

This how you can get your expected output.

StringBuilder csv = new StringBuilder();

using (var r = ChoJSONReader.LoadText(json)
    .WithJSONPath("$..getUsers[*]")
    )
{
    using (var w = new ChoCSVWriter(csv)
        .WithFirstLineHeader()
        .Configure(c => c.MaxScanRows = 2)
        .Configure(c => c.ThrowAndStopOnMissingField = false)
        )
    {
        w.Write(r);
    }
}

Console.WriteLine(csv.ToString());

0reactions
Kunjahamed-Pcommented, Jan 15, 2020

It’s working

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON to CSV conversion using ChoETL Some array value ...
While converting, I am able to get all the values out of the JSON and into the CSV. However, array values are missing...
Read more >
Convert json file to CSV file in C# - CodeProject
Am trying to convert json to csv. the json that i use is a complex json with deep nested arrays and child objects....
Read more >
Cinchoo ETL - Merging JSON Array Values to Single CSV ...
In this tip, you will learn how to merge JSON array values to single CSV column using Cinchoo ETL framework. It is very...
Read more >
CSVJSON - CSVJSON
The easy, confidential online data converter. Csvjson helps you quickly convert popular data formats to the format you need. Data pasted and converted ......
Read more >
Cinchoo ETL — CSV Reader. Simple CSV file reader for .NET
This article talks about using CSVReader component offered by ChoETL framework. It is a simple utility class to extract CSV data from file...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found