Is there any existing standard frame works to do this? currently i'm converting csv fields to an array then converting to one more array as JSON format, which is very slow. I want to know if there are any standard frameworks available or any other fast method out there?
Asked
Active
Viewed 7,650 times
2
-
Possible duplicate: http://stackoverflow.com/questions/662859/converting-csv-xls-to-json – c0d3junk13 Aug 09 '14 at 10:04
-
I posted variant which should be faster because it doesn't use multiple arrays (see below). Hope that helps. Beside that it is always helpful when you post essential parts of your code regarding to the question. – witrin Aug 10 '14 at 10:59
-
You have a slight problem in that there's not really a standard for CSV files. – Hot Licks Aug 11 '14 at 18:28
1 Answers
3
The library jscons allows you to do so without an additional array for the CSV data:
#include <iostream>
#include "jsoncons/json.hpp"
#include "jsoncons_ext/csv/csv.hpp"
using namespace std;
using namespace jsoncons;
using namespace jsoncons::csv;
int main(int argc, char** argv) {
json result = decode_csv<json>(cin);
cout << result << endl;
return 0;
}
On Linux you could use it like this (assume convert
is your compiled binary):
$ cat data.csv | convert
On Windows it might be:
> convert < data.csv
When you want to use this with file names you could do it like this:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdexcept>
#include "jsoncons/json.hpp"
#include "jsoncons_ext/csv/csv.hpp"
using namespace std;
using namespace jsoncons;
using namespace jsoncons::csv;
int main(int argc, char** argv) {
try {
vector<string> arguments(argv + 1, argv + argc);
if (arguments.size() != 2)
throw runtime_error("usage: convert <input> <output>");
ifstream input(arguments[0]);
ofstream output(arguments[1]);
if (!input.is_open())
throw runtime_error(arguments[0] + ": open failed");
if (!output.is_open())
throw runtime_error(arguments[1] + ": open failed");
json result = decode_csv<json>(input);
output << result << endl;
}
catch (exception& e) {
cout << e.what() << endl;
}
return 0;
}
You should call it like this:
$ convert data.csv data.json
To compile all that you have to put the folders jsoncons
and jsoncons_ext
into your include directory.
-
Hi, thanks for the code? but is this code working? How do you input the csv file name? I tried using visual studio 2013, i'm giving the file name after running it, but it's not working(its just stays the same, doesn't seem to do anything), if its possible could you give compiling and executing instructions, i'm new to this. and possible input/output. – goal4321 Aug 11 '14 at 15:54
-
there is a problem with this, this is not taking care of numbers, everything here is a string. my csv file is(name,lname,age\n xyz,p,20\n abc,q,25) it will give [["name","lname ","age"],["xyz","p","20"],["abc","q","25"]], there shouldn't be double quotes for age, and I want the data in the form of {"name":"xyz","lname":"p","age":20} etc. does this library support that? – goal4321 Aug 12 '14 at 09:13
-
There are docs for that library. Also you're be able to override it easily. Maybe a new question ;). – witrin Aug 12 '14 at 09:34
-
For now it is not worked. json_deserializer.hpp - it is not found ( – Olexandr Minzak Aug 30 '19 at 10:57
-
@OlexandrMinzak See https://github.com/danielaparker/jsoncons/blob/09b742516c2fbb76f6be61da5f3bf5ca89d37027/ChangeLog.md#v0994. – witrin Aug 30 '19 at 23:08
-
1