Click here to Skip to main content
15,031,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on converting JSON files into CSV using ChoETL .
So far the converting is working fine!

My code is :

C#
var sampleJson = File.ReadAllText(@"C:\cpi\log\V510ResultsInfo\cpi001.json");  
                StringBuilder stringBuilder = new StringBuilder();
                using (var machineheight = ChoJSONReader.LoadText(sampleJson).WithJSONPath("$..devices"))
                {
                    using (var w = new ChoCSVWriter(stringBuilder).WithFirstLineHeader()
                        .Configure(c => c.MaxScanRows = 1)
                        .Configure(c => c.ThrowAndStopOnMissingField = false))
                    {
                        w.Write(machineheight);
                    }
                }   
                File.WriteAllText(@"C:\cpi\log\V510ResultsInfo\convertedjson.csv", stringBuilder.ToString());

What this does is that it basically just converts everything (the whole json file). But I'm looking into something more specific. Something in the 6th node to be exact.

so far :
C#
("$..devices")
is working for the 1st-3rd node.. But my "actual_Height(um)" is the 6th node :

"v510_results" - "devices" - "details" - "roi" - "display" - "actual_Height(um)"

I tried :
C#
("$..actual_Height(um)")
and it's not working..

I feel like the problem is at :

C#
using (var w = new ChoCSVWriter(stringBuilder).WithFirstLineHeader()

But i can't seem to fix it..

Could really use some help T_T

What I have tried:

var sampleJson = File.ReadAllText(@"C:\cpi\log\V510ResultsInfo\cpi001.json");
    
                StringBuilder stringBuilder = new StringBuilder();
                using (var machineheight = ChoJSONReader.LoadText(sampleJson).WithJSONPath("$..devices"))
                {
                    using (var w = new ChoCSVWriter(stringBuilder).WithFirstLineHeader()
                        .Configure(c => c.MaxScanRows = 1)
                        .Configure(c => c.ThrowAndStopOnMissingField = false))
                    {
                        w.Write(machineheight);
                    }
                }
                File.WriteAllText(@"C:\cpi\log\V510ResultsInfo\convertedjson.csv", stringBuilder.ToString());
Posted
Updated 23-May-21 22:00pm
Comments
Richard Deeming 24-May-21 5:01am
   
The best place to ask this question would be at the bottom of the article:
Cinchoo ETL - Quick Start: Converting JSON to CSV File[^]

Or in the GitHub project:
Issues · Cinchoo/ChoETL · GitHub[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900