Header JSON Converting CSV to JSON - c#

Hi im tring to convert CSV String to JSON, but the Header of the JSON has some issues with Encode i think.
Here the code and the Output:
[{...
"Endere_o_4": "",
"Endere_o_5": "",
"Endere_o_6": "",
"C_digo_Postal": "1000-045", ...]}
Expected Result: [{...
"Endereço_4": "",
"Endereço_5": "",
"Endereço_6": "",
"Código_Postal": "1000-045", ...]}
public void MssCSVtoJSON(string ssCSV, out string ssJSON)
{
ssJSON = "";
ChoCSVFileHeaderConfiguration headerConfiguration = new ChoCSVFileHeaderConfiguration(null, new System.Globalization.CultureInfo("pt-PT"));
ChoCSVRecordConfiguration config = new ChoCSVRecordConfiguration();
config.FileHeaderConfiguration = headerConfiguration;
StringBuilder sb = new StringBuilder();
using (var p = ChoCSVReader.LoadText(ssCSV,Encoding.Unicode, config, null).WithFirstLineHeader()) {
using (var w = new ChoJSONWriter(sb)) {
w.Configuration.Encoding = Encoding.Unicode;
w.Write(p);
}
}
ssJSON = sb.ToString();
// TODO: Write implementation for action
} // MssCSVtoJSON

It is known issue, put a fix and pushed ChoETL 1.1.0.5-alpha2 nuget package.
Here is working sample
string csv = #"Endereço_4, Endereço_5
1, 11
2, 22";
StringBuilder output = new StringBuilder();
using (var r = ChoCSVReader.LoadText(csv).WithFirstLineHeader())
{
using (var w = new ChoJSONWriter(output))
w.Write(r);
}
Console.WriteLine(output);
Output:
[
{
"Endereço_4": "1",
"Endereço_5": "11"
},
{
"Endereço_4": "2",
"Endereço_5": "22"
}
]

Related

Hi, how can I access the length of a specific json value, if I have its key. I'm working with C#

using (StreamReader r = new StreamReader(filePath))
{
string json = r.ReadToEnd();
JObject data = JObject.Parse(json);
foreach (var x in data)
{
if (x.Key.Contains(product))
{
Console.WriteLine(x.Key.Contains(product));
// for (int m = 0; m <= x.Value.ToString().Length; m++)
}
}
Console.WriteLine(tempList.Count);
Console.WriteLine("");
}
}
else
{
Console.WriteLine("ERROR: Data not returned by Database...");
return View();
}
}
else
{
Console.WriteLine("ERROR: No Value recieved by client...");
return View();
}
return View();
}
// -------------------------------------------------------------------------------------
// in the above snippet, I want to iterate on x.Value, like x.Value[0], then x.Value[1] and so on.
// The format of my json file is like this...
{
"BOOK": [ "FORMAT", "PAGE", "DIMENSION", "WEIGHT", "PUBLICATIONDATE", "LANGUAGE", "GENRE", "AUTHOR" ],
"COMPUTER": [ "BRAND", "TYPE", "COLOUR", "STORAGE", "CPU", "RAM", "SCREENSIZE", "CPMPUTERTYPE", "DISPLAYTYPE", "PROCESSORCORENUMBER", "SSDSTORAGE", "PORT", "BLUETOOTH", "OPERATINGSYSTEM" ],
"GAME": [ "FORMAT", "GENRE", "RATING", "DEVELOPER", "PUBLISHER" ],
"MOBILE": [ "BRAND", "COLOUR", "STORAGE", "SCREENSIZE", "NETWORK", "PHONEOPERATINGSYSTEM", "SCREENSIZEINCHES", "INTERNALSTORAGE", "BATTERYCAPACITYMAH", "CPU", "RAM", "REARCAMERAMP", "FRONTCAMERAMP", "FLASH", "HEADPHONEJACK", "BLUETOOTH", "RELEASEYEAR" ],
"MOVIE": [ "GENRE", "REGIONCODING", "RATING", "RELEASEYEAR", "FORMAT" ]
....
}
Its bit unclear what you actually want but let me try to help you.
Lets understand your JSON schema first. It has productCategory ("Book","Computer","Game" etc.) and each productCategory has array of properties (For "Book" -> [ "FORMAT", "PAGE", "DIMENSION",...]
So now if you want to iterate through your productCategory and count the length of it, you can do the following:
using (StreamReader r = new StreamReader(filePath))
{
string json = r.ReadToEnd();
JObject data = JObject.Parse(json);
foreach (var productCategory in data)
{
// counts the number of properties like for eg. For "Book" it has 8 properties like "Format","Page" etc.
var count = productCategory.Value.Children().ToList().Count;
Console.WriteLine($"{productCategory.Key} Count: {count}");
}
}
Output:
And if you also want to iterate through properties of each productCategory, you can do the following:
using (StreamReader r = new StreamReader(filePath))
{
string json = r.ReadToEnd();
JObject data = JObject.Parse(json);
foreach (var productCategory in data)
{
// counts the number of properties like for eg. For "Book" it has 8 properties like "Format","Page" etc.
var count = productCategory.Value.Children().ToList().Count;
Console.WriteLine($"{productCategory.Key} Count: {count}");
foreach (var properties in productCategory.Value)
{
//Here you will have properties like "Format","Page" etc..
Console.WriteLine(properties);
}
Console.WriteLine("");
}
}
Output:
Thanks a lot for trying to help me out.
Really appreciate it.
I tried the idea that OhmnioX gave and played around with the code, finally created the following snippet.
using (StreamReader r = new StreamReader(filePath))
{
string json = r.ReadToEnd();
JObject data = JObject.Parse(json);
foreach (var x in data)
{
if (x.Key.ToString().Contains(product))
{
var cnt = x.Value.Children().ToList().Count;
for (int m=0; m<=cnt-1; m++)
{
tempList.Add(x.Value[m].ToString().ToUpper());
}
}
}
}

Write Text file with tab-delimited in Asp.Net Core 2.2

Hi do you have any guides, work aid or step by step how to export to text with tab delimited. Im using Asp.Net Core 2.2 MVC EF. I want to export a list from my table.. I want to have a button where the user click in this DownloadFile Action will trigger.
public IActionResult DownloadFile()
{
var payments = new List<BdoPE>
{
new BdoPE
{
DocDateInDoc = "01/01/2019",
DocType = "DZ",
CompanyCode = "3000",
PosDateInDoc = "01/01/2019",
FiscalPeriod = "01",
CurrentKey = "PHP",
RefDocNum = "Over-The-Counter",
DocHeadT = "BDO",
PosKeyInNextLine = "40",
AccMatNextLine = "11231131",
AmountDocCur = "0000000010050",
ValDate = "01/01/2019",
AssignNum = "EEA",
ItemText = "1000136212 ",
PosKeyInNextLine2 = "15",
AccMatNextLine2 = "0115027FF",
AmountDocCur2 = "0000000010050",
BaseDateDueCal = "01/01/2019",
ItemText2 = "1000136212"
},
};
// I want this part to let the user select where they want to save the text file.
using (var writer = new StreamWriter("path\\to\\file.txt")) // not static location like this one.
using (var csv = new CsvWriter(writer))
{
csv.WriteHeader<BdoPE>();
csv.WriteRecord(payments);
}
// where should i put the delimiter part?
return;
}
You will need to setup the CsvWriter with a Configuration.
Thus, your code needs only a slight change:
[...]
var configuration = new CsvHelper.Configuration.Configuration();
configuration.Delimiter = '\t';
using (var csv = new CsvWriter(writer, configuration))
{
csv.WriteHeader<BdoPE>();
csv.WriteRecord(payments);
}
[...]
I use the code below to set the Delimiter using CsvHelper.
var config = new CsvConfiguration(CultureInfo.CurrentCulture)
{
Delimiter = "\t"
};

c# convert json string to .csv

I am trying to convert a json string, containing array elements, to a .csv file. Below is the json string format:
{"inputFile": [["Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10"], ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"], ["K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"]]}
solved. Thanks heyNow.
dynamic dynObj = JsonConvert.DeserializeObject(json);
var rowElements = new List<string>();
foreach (var data in dynObj.inputFile)
{
var row = new List<string>();
foreach (var dataItem in data)
{
var item = Convert.ToString(dataItem);
row.Add(item);
}
rowElements.Add( String.Join(",", row)+"\n");
}
var CSV = String.Join("",rowElements);
Console.WriteLine(CSV);
For RaJN:
Updated code to replace json file path to json string:
StringBuilder msg = new StringBuilder();
using (var p = ChoJSONReader.LoadText(jsonString)
.WithJSONPath("$.inputFile[*]")
)
{
using (var w = new ChoCSVWriter(msg))
{
w.Write(p);
}
Console.WriteLine(msg.ToString());
}
try with newtonsoft jsonparser.
If you can get your JSON as a string..
dynamic dynObj = JsonConvert.DeserializeObject(jsonString);
string CSV = "";
foreach (var data in dynObj.inputFile)
{
List<string> row = new List<string>();
foreach(var dataItem in data)
{
row.Add(dataItem);
}
CSV+=string.Join(row, ",");
}
You will get 1 giant string containing all the values as a CSV.
Let us know if this is what you want.
Here is how you can generate CSV from your JSON file using Cinchoo ETL
StringBuilder msg = new StringBuilder();
using (var p = new ChoJSONReader("*** YOUR JSON FILE PATH ***")
.WithJSONPath("$.inputFile[*]")
)
{
using (var w = new ChoCSVWriter(msg))
{
w.Write(p);
}
Console.WriteLine(msg.ToString());
}
Output:
Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8,Column9,Column10
A,B,C,D,E,F,G,H,I,J
K,L,M,N,O,P,Q,R,S,T
UPDATE:
string json = #"
{
""inputFile"": [
[""Column1"", ""Column2"", ""Column3"", ""Column4"", ""Column5"", ""Column6"", ""Column7"", ""Column8"", ""Column9"", ""Column10""],
[ ""A"", ""B"", ""C"", ""D"", ""E"", ""F"", ""G"", ""H"", ""I"", ""J"" ],
[ ""K"", ""L"", ""M"", ""N"", ""O"", ""P"", ""Q"", ""R"", ""S"", ""T"" ]
]
}";
StringBuilder msg = new StringBuilder();
using (var p = ChoJSONReader.LoadText(json)
.WithJSONPath("$.inputFile[*]")
)
{
using (var w = new ChoCSVWriter(msg))
{
w.Write(p);
}
Console.WriteLine(msg.ToString());
}

csv to json file format

I want to convert my csv file into .json format using c#. here what i have tried:
var lines = #"text,intentName,entityLabels
1,2,null
2,1,null".Replace("\r", "").Split('\n');
var csv = lines.Select(l => l.Split(',')).ToList();
var headers = csv[0];
var dicts = csv.Skip(1).Select(row => Enumerable.Zip(headers, row,
Tuple.Create).ToDictionary(p => p.Item1, p => p.Item2)).ToArray();
string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(dicts);
Result1.Text = json;
The result is :
[
{
"text":" 1",
"intentName":"2",
"entityLabels":"null"
},
{
"text":"2",
"intentName":"1",
"entityLabels":"null"
}
]
it almost like I expected, however I want to make if the entityLabels column is null, then it replace into []. so the output that I expecting is:
[
{
"text":" 1",
"intentName":"2",
"entityLabels":[]
},
{
"text":"2",
"intentName":"1",
"entityLabels":[]
}
]
anyone know how to do it?
With external lib Cinchoo ETL - an open source library, you can convert CSV --> JSON with the expected format as below
Method 1:
string csv = #"text,intentName,entityLabels
1,2,null
2,1,null
";
StringBuilder sb = new StringBuilder();
using (var p = ChoCSVReader.LoadText(csv)
.WithFirstLineHeader()
.WithField("text")
.WithField("intentName")
.WithField("entityLabels", fieldType: typeof(int[]), nullValue: "null")
)
{
using (var w = new ChoJSONWriter(sb)
)
w.Write(p);
}
Console.WriteLine(sb.ToString());
Sample fiddle: https://dotnetfiddle.net/5M7fFX
Method 2:
string csv = #"text,intentName,entityLabels
1,2,null
2,1,null
";
StringBuilder sb = new StringBuilder();
using (var p = ChoCSVReader.LoadText(csv)
.WithFirstLineHeader()
.WithField("text")
.WithField("intentName")
.WithField("entityLabels", valueConverter: (o) => new int[] { })
)
{
using (var w = new ChoJSONWriter(sb)
)
w.Write(p);
}
Console.WriteLine(sb.ToString());
Sample fiddle: https://dotnetfiddle.net/gOX3FJ
Output:
[
{
"text": "1",
"intentName": "2",
"entityLabels": []
},
{
"text": "2",
"intentName": "1",
"entityLabels": []
}
]
Hope it helps.
Don't try to use string operations to convert from one data type to another.
Instead use an actual CSV parsing library like csvhelper (available on NuGet) to deserialise the CSV into objects, and then re-serialise that same data as JSON using a JSON serializer.

Parse dynamic json as simple as possible

I am not much of a C# Programmer so I am fairly new to this, I would like to parse the sample JSON below, I have been using the code:
WebClient client = new WebClient();
string getString = client.DownloadString(url);
dynamic j = JsonConvert.DeserializeObject(getString);
var k = j.rgDescriptions;
dynamic m = JsonConvert.DeserializeObject(k);
foreach (var c in m.descriptions)
{
Console.WriteLine(c);
}
I get error in deserialize of k, I am not sure if I am at the right path though. How do I get the "classid" w/o getting the value of their parent, because it is dynamic and not named, it is a Unique ID.
{
"success": true,
"rgDescriptions": {
"671219543": {
"id": "671219543",
"classid": "253033065",
"instanceid": "93973071",
"amount": "1",
"pos": 274
},
"707030894": {
"id": "707030894",
"classid": "166354998",
"instanceid": "0",
"amount": "1",
"pos": 277
},
Update:
I used this code:
WebClient client = new WebClient();
string getString = client.DownloadString(url);
var jo = JObject.Parse(getString);
var data = (JObject)jo["rgDescriptions"];
foreach (var item in data)
{
Console.WriteLine("{0}: {1}", item.Key, item.Value);
}
I could get what I wanted now, but I need to parse each value. Is there a better way?
You could use JSON.NET and JSONPath to query the incoming JSON, examples here and here
The code below extracts every classid for each object in rgDescriptions
//...
WebClient client = new WebClient();
string getString = client.DownloadString(url);
var obj = JObject.Parse(getString);
var classIds = obj.SelectTokens("$.rgDescriptions.*.classid").Select(x => x.Value<string>()).ToList(); //[253033065,166354998]
//Class ID Where...
var idToSearch = "671219543";
var classId = obj.SelectToken("$.rgDescriptions['" + idToSearch + "']..classid").Value<string>();
//...

Categories

Resources