Looking for a method to transform the content of a JSON file where each field is replaced with an array?
{
"ID": "GO:0051345",
"Description": "positive regulation of hydrolase activity",
"GeneRatio": "3/5",
"BgRatio": "464/15405",
"pvalue": 0.000259498331261799,
"p_adjust": 0.0446337129770295,
"qvalue": 0.0169356805665595,
"geneID": "Ptpa/Wdr35/Dnajb11",
"Count": 3
}
The following code accomplishes this task:
GO_data = fs.readFileSync('my_data.json');
GO_data = JSON.parse(GO_data);
var data_header = Object.keys(GO_data);
// Enumerate its property names
for (var prop in GO_data) {
var arr = [];
arr.push(GO_data[prop]);
GO_data[prop] = arr;
}
// Write the converted JSON
fs.writeFileSync('my_data_arr.json', JSON.stringify(GO_data), 'utf8');
After transformation, the content of 'my_data_arr.json' appears as desired:
{
"ID": [
"GO:0051345"
],
"Description": [
"positive regulation of hydrolase activity"
],
"GeneRatio": [
"3/5"
],
"BgRatio": [
"464/15405"
],
"pvalue": [
0.000259498331261799
],
"p_adjust": [
0.0446337129770295
],
"qvalue": [
0.0169356805665595
],
"geneID": [
"Ptpa/Wdr35/Dnajb11"
],
"Count": [
3
]
}
If you have any suggestions for a better or faster approach, please share!