-
Notifications
You must be signed in to change notification settings - Fork 0
/
json2csv.h
194 lines (165 loc) · 4.21 KB
/
json2csv.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <json/json.h>
#include <stdio.h>
#include <string.h>
char* keys[2560];
const char* values[2560];
static void takeKeys(char* key);
static void takeValues(const char* value);
static void jsonToCsv(char *out);
void print_json_value(json_object *jobj,char* key){
enum json_type type;
//printf("type: ",type);
type = json_object_get_type(jobj); /*Getting the type of the json object*/
switch (type) {
case json_type_boolean:
// printf("json_type_boolean\n");
// printf("value: %s\n", json_object_get_boolean(jobj)? "true": "false");
break;
case json_type_double:
// printf("json_type_double\n");
// printf("value: %lf\n", json_object_get_double(jobj));
break;
case json_type_int:
//printf("json_type_int\n");
//printf("value: %d\n", json_object_get_int(jobj));
takeKeys(key);
takeValues(json_object_get_string(jobj));
break;
case json_type_string:
//printf("json_type_string\n");
// printf("value: %s\n", json_object_get_string(jobj));
takeKeys(key);
takeValues(json_object_get_string(jobj));
break;
}
}
void json_parse_array(json_object *jobj, char *key) {
void json_parse(json_object * jobj);
enum json_type type;
json_object *jarray = jobj; /*Simply get the array*/
if(key) {
jarray = json_object_object_get(jobj, key); /*Getting the array if it is a key value pair*/
}
int arraylen = json_object_array_length(jarray); /*Getting the length of the array*/
//printf("Array Length: %d key: %s\n",arraylen,key);
int i;
json_object * jvalue;
for (i=0; i< arraylen; i++){
jvalue = json_object_array_get_idx(jarray, i); /*Getting the array element at position i*/
type = json_object_get_type(jvalue);
if (type == json_type_array) {
json_parse_array(jvalue, NULL);
}
else if (type != json_type_object) {
//printf("value[%d]: ",i);
print_json_value(jvalue, key);
}
else {
json_parse(jvalue);
}
}
}
/*Parsing the json object*/
void json_parse(json_object * jobj) {
enum json_type type;
json_object_object_foreach(jobj, key, val) { /*Passing through every array element*/
//printf("type: ",type);
type = json_object_get_type(val);
switch (type) {
case json_type_boolean:
break;
case json_type_double:
break;
case json_type_int:
print_json_value(val, key);
break;
case json_type_string:
print_json_value(val, key);
break;
case json_type_object:
//printf("json_type_object\n");
jobj = json_object_object_get(jobj, key);
json_parse(jobj);
break;
case json_type_array:
//printf("type: json_type_array, ");
json_parse_array(jobj, key);
break;
}
}
}
static void takeValues(const char* value)
{
int i = 0;
while(values[i])
{
i++;
}
values[i] = value;
i = 0;
}
static void takeKeys(char* key)
{
int i = 0, flag = 0;
while(keys[i])
{
if(strcmp(keys[i], key) == 0)
{
flag=1;
}
i++;
}
if(flag == 0)
{
keys[i] = key;
}
i = 0;
}
static void jsonToCsv(char *out)
{
FILE *fp = fopen(out,"w");
int i = 0, j = 0;
while(keys[i])
{
if(keys[i + 1] == NULL)
{
fprintf(fp, "%s\n", keys[i]);
}
else
{
fprintf(fp, "%s,", keys[i]);
}
i++;
}
while(values[j])
{
j++;
if(j % i == 0)
{
fprintf(fp, "%s\n", values[j-1]);
}
else
{
fprintf(fp, "%s,", values[j-1]);
}
}
fclose(fp);
}
static void convertJsontoCsv(char *in, char *out) {
char* data[2000];
char* token;
char buffer[2000];
FILE *fp = fopen(in,"r");
int i=0;
while (fgets(buffer,2000, fp))
{
token = buffer;
data[i] = malloc(strlen(token));
strcpy(data[i],token);
//printf("test : %s\n",data[i]);
json_object * jobj = json_tokener_parse(data[i]);
json_parse(jobj);
i++;
}
jsonToCsv(out);
}