Skip to content

Instantly share code, notes, and snippets.

@dipu-bd
Created February 22, 2017 14:41
Show Gist options
  • Save dipu-bd/dc6a3a76e5a4428f8d90f6fcb16294d5 to your computer and use it in GitHub Desktop.
Save dipu-bd/dc6a3a76e5a4428f8d90f6fcb16294d5 to your computer and use it in GitHub Desktop.
Converts TSV file to JSON (not for general purpose)
/*
* Converts TSV file to JSON file.
*
* author: Sudipto Chandra Das
* license: MIT
*
*/
#include <bits/stdc++.h>
using namespace std;
char inp[10000];
const char SEP = '\t';
const char* prop[] = {
"title",
"desc",
"h1",
"h2_1",
"h2_2"
};
int main()
{
freopen("35 URLs.tsv", "r", stdin);
freopen("metaInfo.json", "w", stdout);
bool beg = true;
printf("{\n");
while(gets(inp))
{
if(beg) beg = false;
else printf(",\n");
string tmp = "";
int len = strlen(inp);
int c = 0;
for(int i = 0; i < len; ++i)
{
if(inp[i] == SEP)
{
if(c == 0)
{
printf("%4c\"%s\": {\n", ' ', tmp.data());
tmp = "";
c++;
}
else if(c % 2 == 1)
{
printf("%8c\"%s\": \"%s\",\n", ' ', prop[c / 2], tmp.data());
tmp = "";
c++;
}
else
{
tmp = "";
c++;
}
}
else if(inp[i] != '\n' && inp[i] != '\r')
{
tmp.push_back(inp[i]);
}
}
// last part
printf("%8c\"%s\": \"%s\"\n", ' ', prop[c / 2], tmp.data());
tmp = "";
printf("%4c}", ' ');
}
printf("\n}\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment